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 : 15287 | Status : [Member] | Replies : 2
Hi sir,
Tell me the difference between modelstate.addmodelerror with or without key in mvc.


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

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

With regards,


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

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.");


Mark This Response as Answer

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

Login to post response