Asp.net mvc 如何验证从ASP.NET MVC3中的数据库填充的dropdownlist?
我有两张桌子,个人和国籍。此人通过NationalityID将FK添加到国籍表中。在我的CreatePerson表单中,我有一个下拉列表,其中填充了NationalityID和NationalityDescription。对于使用开发者工具栏等将发布的值更改为无效的NationalityID的人,验证此下拉列表的最佳方法是什么?Asp.net mvc 如何验证从ASP.NET MVC3中的数据库填充的dropdownlist?,asp.net-mvc,asp.net-mvc-3,asp.net-mvc-validation,Asp.net Mvc,Asp.net Mvc 3,Asp.net Mvc Validation,我有两张桌子,个人和国籍。此人通过NationalityID将FK添加到国籍表中。在我的CreatePerson表单中,我有一个下拉列表,其中填充了NationalityID和NationalityDescription。对于使用开发者工具栏等将发布的值更改为无效的NationalityID的人,验证此下拉列表的最佳方法是什么? 我一直在考虑在viewmodel中使用System.DataAnnotations.AssociationAttribute,但我不确定这是否正是我所需要的 这种验证应
我一直在考虑在viewmodel中使用System.DataAnnotations.AssociationAttribute,但我不确定这是否正是我所需要的 这种验证应该由业务层执行。例如:
[HttpPost]
public ActionResult Update(int nationalityId, int personId)
{
string error;
if (!Repository.TryUpdatePersonNationality(personId, nationalityId, out error))
{
// The business layer failed to perform the update
// due to FK constraint violation => add the error to model state
ModelState.AddModelError(nationalityId, error);
// redisplay the form so that the user can fix the error
return View();
}
return RedirectToction("Success");
}
这种验证应该由业务层执行。例如:
[HttpPost]
public ActionResult Update(int nationalityId, int personId)
{
string error;
if (!Repository.TryUpdatePersonNationality(personId, nationalityId, out error))
{
// The business layer failed to perform the update
// due to FK constraint violation => add the error to model state
ModelState.AddModelError(nationalityId, error);
// redisplay the form so that the user can fix the error
return View();
}
return RedirectToction("Success");
}