difference between modelstate.addmodelerror with or without key in mvc.

Posted by Prabu_Spark under ASP.NET MVC on 7/15/2014 | Points: 10 | Views : 18454 | Status : [Member] | Replies : 2
Hi sir,
Tell me the difference between modelstate.addmodelerror with or without key in mvc.


@Html.ValidationMessageFor(m=>m.username)


ModelState.AddModelError("username","Username cannot be empty")

ModelState.AddModelError("","Username cannot be empty")

With regards,
J.Prabu.
[Email:prbspark@gmail.com]



Responses

Posted by: Bandi on: 7/15/2014 [Member] [MVP] Platinum | Points: 25

Up
0
Down
To display customized error message, 'Key' will be used, whereas without Key in the method will display generic error message...

The below code adds a custom error message for each column that has database values different from what the user entered on the Edit page:

if (databaseValues.Name != currentValues.Name)
ModelState.AddModelError("Name", "Current value: " + databaseValues.Name);
// ...


A longer error message explains what happened and what to do about it:

ModelState.AddModelError(string.Empty, "The record you attempted to edit "
+ "was modified by another user after you got the original value. The"
+ "edit operation was canceled and the current values in the database "
+ "have been displayed. If you still want to edit this record, click "
+ "the Save button again. Otherwise click the Back to List hyperlink.");


reference:
http://www.asp.net/mvc/tutorials/getting-started-with-ef-using-mvc/handling-concurrency-with-the-entity-framework-in-an-asp-net-mvc-application

Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

Prabu_Spark, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response