我有超过40个领域的大观点。其中一些字段是下拉列表,我填充了一个selectlistitems列表。
我在我的viewmodel上使用注释,以使其中一些需要。
现在我想进行ModelState.IsValid
检查,如果出现错误,请返回原始视图,但有错误。
问题是,如果我只是制作一个return View(model)
,我的所有下拉列表都会使网站崩溃,因为需要再次填充它们才能加载页面。
所以我的问题是:当modelstate无效时,我如何处理返回。
我的控制器:
public ActionResult CreateSelling(SellingAdViewModel model)
{
if (ModelState.IsValid)
{
SellingAdvert sellingAdvert = setSellingAd(model);
var stored_advert = sellingAdvertService.Create(sellingAdvert);
if (User != null && User.Identity.IsAuthenticated)
{
if (model.AcceptPartner)
{
notifyPartner(stored_advert);
}
return RedirectToAction("Upgrade", "Ads", new { AdvertID = stored_advert.Id });
}
else
{
return RedirectToAction("ActivateAnonymous", "Ads", new { AdvertID = stored_advert.Id, anonymousId = model.UserId, AdvertType = "selling" });
}
}
return View(model);
}
我的viewmodel(我有一个SellingAdViewModel,派生自这个并添加更多属性):
public class BasicAdViewModel
{
public int SectorId { get; set; }
public Guid UserId { get; set; }
public bool IsAnonymousUser { get; set; }
public int AdvertId { get; set; }
[DisplayName("Titel:")]
[Required(ErrorMessage = "Titel skal udfyldes")]
public string Headline { get; set; }
[DisplayName("Beskrivelse:")]
[StringLength(50, ErrorMessage = "Beskrivelsen minimum fylde 50 karakterer")]
public string Description { get; set; }
[DisplayName("Søgeord:")]
public string SearchWords { get; set; }
[DisplayName("Undertitel:")]
public string Subtitle { get; set; }
[DisplayName("Type af drift")]
public List<SelectListItem> OperationTypes { get; set; }
[Required]
public int SelectedOperationTypeId { get; set; }
[Required]
public int SelectedSectorId { get; set; }
public IEnumerable<GroupedSelectListItem> Sectors { get; set; }
}
首先设置我的下拉列表:
我的模型具有List<SelectListItem>
属性,我通过使用几个辅助方法填充它们:
SellingAdViewModel model = new SellingAdViewModel()
{
BusinessEntityTypes = ModelListHelpers.GetBusinessEntityTypes(),
FoundedYears = ModelListHelpers.GetFoundedYears(null),
ReasonForSale = ModelListHelpers.GetReasonForSale(),
RevenuePrediction = ModelListHelpers.GetRevenuePrediction(),
RevenueStatus = ModelListHelpers.GetRevenueStatus(),
OperationTypes = ModelListHelpers.GetOperationTypes(),
Region = ModelListHelpers.GetRegions(),
Turnover = ModelListHelpers.Turnovers(),
OperatingIn = ModelListHelpers.OperatingIn(),
AmountOfEmployees = ModelListHelpers.GetAmountOfEmployees()
};
答案 0 :(得分:3)
我建议你这个解决方案在你的GET动作期间将你的模型放在TempData
中如果模型状态无效,你可以做这样的任务
public ActionResult CreateSelling(SellingAdViewModel model)
{
if (ModelState.IsValid)
{
SellingAdvert sellingAdvert = setSellingAd(model);
var stored_advert = sellingAdvertService.Create(sellingAdvert);
if (User != null && User.Identity.IsAuthenticated)
{
if (model.AcceptPartner)
{
notifyPartner(stored_advert);
}
return RedirectToAction("Upgrade", "Ads", new { AdvertID = stored_advert.Id });
}
else
{
return RedirectToAction("ActivateAnonymous", "Ads", new { AdvertID = stored_advert.Id, anonymousId = model.UserId, AdvertType = "selling" });
}
}
model.YourList = TempData.Peek("YourList");
return View(model);
}