我有 2 张桌子,个人和国籍。Person 通过 NationalityID 对 Nationality 表有一个 FK。在我的 Create Person 表单中,我有一个包含 NationalityID 和 NationalityDescription 的下拉列表。验证此下拉列表以处理使用开发人员工具栏等将发布的值更改为无效 NationalityID 的人的最佳方法是什么?我一直在研究在视图模型中使用 System.DataAnnotations.AssociationAttribute 但我不确定这是否正是我所需要的。
问问题
626 次
1 回答
1
这种验证应该由业务层执行。例如:
[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");
}
于 2011-01-13T10:13:22.540 回答