2011-01-12 35 views
1

我有2張桌子,人物和國籍。人員通過國籍ID獲得國籍表的FK。在我的創建人形式中,我有一個填充了國籍ID和國籍說明的下拉菜單。驗證此下拉菜單以處理使用開發人員工具欄等將更改張貼值更改爲無效國籍ID的人的最佳方法是什麼?我一直在尋找在ViewModel中使用System.DataAnnotations.AssociationAttribute,但我不知道這是否是我所需要的。如何驗證從ASP.NET MVC3中的數據庫填充的下拉列表?

回答

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"); 
} 
相關問題