Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ModelState.AddModelError on form level with ASP.NET MVC 2 RC

In the previous versions on ASP.NET MVC, you could use ModelState.AddModelError("**_FORM**", "error") to make an error on the form-level instead of a property-level. This seems not to work anymore with the new RC of ASP.NET MVC 2.

Is there another way how to do it? I use <%= Html.ValidationSummary() %> at my View-file...

like image 344
Jeff Maes Avatar asked Feb 08 '10 13:02

Jeff Maes


People also ask

What is ModelState AddModelError in MVC?

ModelState.AddModelError method accepts two parameters. Key – First parameter, if empty shows error written in the 2nd parameter at place where @Html. ValidationSummary(true, "", new { @class = "text-danger" }) is written in the view.

How ModelState works in MVC?

When MVC creates the model state for the submitted properties, it also iterates through each property in the view model and validates the property using attributes associated to it. If any errors are found, they are added to the Errors collection in the property's ModelState .

What does ModelState AddModelError do?

AddModelError(String, String)Adds the specified error message to the errors collection for the model-state dictionary that is associated with the specified key.


1 Answers

I personally use AddModelError("" - I don't know where does _FORM come from? - and ValidationSummary() is no black magic - I use my own helper instead that, for example, includes specific (user-specific) exceptions messages into output. The helper is very easy to write - a simple foreach loop over ModelState - so you may consider writing your own one. There you're free to show form-level, property-level, or whatever you like.

like image 173
queen3 Avatar answered Oct 21 '22 18:10

queen3