1

I have 2 tables, Person and Nationality. Person has an FK to the Nationality table via NationalityID. In my Create Person form, I've got a dropdown that's populated with NationalityID and NationalityDescription. What's the best way to validate this dropdown to deal with people using developer toolbars etc to change the posted value to an invalid NationalityID? I've been looking at using System.DataAnnotations.AssociationAttribute in a viewmodel but I'm not sure if this is quite what I need.

1 Answer 1

1

This kind of validation should be performed by the business layer. For example:

[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");
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.