2011-01-12 2 views
1

나는 2 개의 테이블 인력과 국적을 가지고 있습니다. 사람은 국적을 통해 국적표에 FK를 가지고 있습니다. 내 Create Person 양식에는 NationalityID 및 NationalityDescription으로 채워진 드롭 다운이 있습니다. 이 드롭 다운의 유효성을 검사하여 개발자 도구 모음 등을 사용하여 게시 된 가치를 잘못된 NationalityID로 변경하는 사람들을 다루는 가장 좋은 방법은 무엇입니까? System.DataAnnotations.AssociationAttribute를 viewmodel에서 사용하고 있습니다. 그러나 이것이 필요한지 확실하지 않습니다.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"); 
} 
관련 문제