제품 및 카테고리의 SelectList가 포함 된 viewmodel이 있습니다.ViewModel의 유효성 검사가 예외를 throw합니다.
public class AdFormViewModel
{
public AmericanAds.Model.Ad Ad { get; set; }
public SelectList Categories { get; set; }
public AdFormViewModel(AmericanAds.Model.Ad ad, SelectList categories)
{
Ad = ad;
Categories = categories;
}
}
새 제품을 추가 할 때 범주 드롭 다운에 대한 유효성 검사가 실패하면 아래 오류 메시지가 표시됩니다.
사전에 전달 된 모델 항목의 형식은 'AmericanAds.Model.Ad'이지만이 사전에는 'AmericanAds.Controllers.AdFormViewModel'유형의 모델 항목이 필요합니다.
다음은 작성 작업을위한 컨트롤러입니다.
public ActionResult Create()
{
AdFormViewModel data = new AdFormViewModel(
null,
new SelectList(_repository.CategoryList().ToList(), "CategoryId", "CategoryName")
);
return View(data);
}
//
// POST: /Ad/Create
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Create(Ad ad)
{
if (ModelState.IsValid)
{
try
{
_repository.AddAd(ad);
return RedirectToAction("Index");
}
catch
{
return View(ad);
}
}
else
{
return View(ad);
}
}
무엇이 누락 되었습니까?
내가 알기로, 나는 ASP.Net MVC에 매우 익숙하다.
감사합니다. 당신의 Create
보기 유형 AdFormViewModel
의하지만 Create
행동합니다 ([AcceptVerbs(HttpVerbs.Post)]
속성을 가진 것)이 유형 Ad
의 모델을 (이 return View(ad)
을 말한다 어디 라인 참조) 반환의 모델을 필요로하기 때문에
이 asp.net-mvc 2입니까? 그렇지 않으면 어떤 검증 엔진을 실행하고 있습니까? 보기 마크 업과 실패한 작업을 보여줄 수 있습니까? –
그것은 mvc 1.0입니다. 게시물에 몇 가지 코드를 추가하겠습니다. –
호기심 때문에이 시나리오에서 드롭 다운에 대한 검증이 어떻게 실패 할 수 있습니까? 때로는 효과가 있고 때로는 실패합니까? 그렇다면 매번 입력은 무엇입니까? 항상 실패합니까? 그렇다면 어떤 라인에 있습니까? –