How to check for Model validation errors in asp.net mvc? -


How to check for a model key with a modelState errors (key model's field key)

If you have not done so yet, check out this wiki article.

Keep in mind that your view only is considered in-charge of displaying data. As such, you should at least try to keep the quantity of logic in your view, if possible, handling modelstate errors in your controller (like ModelState errors are the result of failed model binding attempts):

  Public Sector Home Controller: Controller {Public Action Index () {if (ModelState.IsValid) {Return Redirect ToAction ("where"); } See Return (); }}  

If you have to handle modelstate errors in your view, then you can:

  & lt;% if (ViewData.ModelState IsValidField ("key")) {%> Model state valid & lt;%}% & gt;  

But keep in mind that you can accomplish the same thing with your controller and thus remove unnecessary logic from your scene. To do this, you can place modelstate logic in your controller:

  Public Class Home Controller: Controller {Public Performance Index} (if (! ModelState. ISWillfield ("Key")) {TempData ["Error Message"] = "Not Valid"}} Else {TempData ["ErrorMessage"] = "Valid";} Return View ();}}  

And then, In your opinion, you can refer to the Tempadata message, which is from the point of view of any unnecessary logic The Cha:

  & lt;% = TempData [ "message"]% & gt;  

Comments

Popular posts from this blog

windows - Heroku throws SQLITE3 Read only exception -

lex - Building a lexical Analyzer in Java -

python - rename keys in a dictionary -