How to check from inside the View if there are any ModelState errors for specific key (key is the field key of the Model)
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
If you haven’t yet, check out this wiki article on the MVC pattern.
Keep in mind that your view is only supposed to be in charge of displaying data. As such, you should try to keep the amount of logic in your view to a minimum. If possible, then, handle ModelState errors (as ModelState errors are the result of a failed model binding attempt) in your controller:
If you have to handle ModelState errors in your view, you can do so like this:
But keep in mind that you can accomplish the same thing with your controller and thus remove the unnecessary logic from your view. To do so, you could place the ModelState logic in your controller:
And then, in your view, you can reference the TempData message, which relieves the view of any unnecessary logic-making: