Validation Messages are not showing when Editing an Item

Apr 1, 2012 at 5:05 PM
Edited Apr 1, 2012 at 5:40 PM

In the example provided in there are 2 partial views ToDoDisplayItem.ascx and ToDoEditItem.ascx. I made the following changes to the example to get validation messages to show:


<%: Html.ValidationMessageFor(m => m.Name)%><%: Html.DisplayField(m => m.Name) %>


<%: Html.ValidationMessageFor(m => m.Name)%><%: Html.TextBoxFor(m => m.Name) %>

I basically removed the "*" as a second parameter from ValidationMessageFor


[Required(ErrorMessage = "Name required "), CanSort, Display(Name = "Name")]
 public object Name { get; set; }


So the problem I have is when editing an item the validation error message does not show but it works properly when adding an item.


After some debugging I realized that replacing Html.DisplayField(m => m.Name) in ToDoDisplayItem with Html.TextBoxFor(m => m.Name, new { style = "readonly:true; border:none; background-color:#fff; background:transparent;" })  I was able to get the error message to show. However, this solution introduced other problems. For example, when I clear the the Name textbox field and hit save the error message shows above the field and it stays there even if you correct the error or hit undo.


So, any ideas on how to get error messages to show for both editing and inserting items

Apr 1, 2012 at 8:30 PM

I don't understand at what code you are referring to, because the blog post you refrenced is a Razor example while the code you reprorted its aspx.

Please let me understand what code are youre ferring to.

There are 3 similar example contained in the BasicTutorialsCode folders: 

  1. A razor example (the one referred in the blog post)
  2. An aspx Mvc 3 example
  3. An Mvc 2 example

There is another razore example too, on advanced filtering...but for sure you are not referring to it

Anyway, I tried the changes you reported in the razor example and it works fine for me. Consider that Require attribute errors are not immediately shown in unobtrusive validation, but they are shown either after you submit the form or after you write something and then leve the textbox at least once. Otherwise the filed is considered "not yet changed by the user" and no error is shown also if the filed is empty. This is a general feature of unobtrusive validation.

If you use the TypedTextBox instead of the simple textbox, the error is always shown immediately, so you might solve substituting the textbox with the TypedTextBox. 

Finally I don't understang why you tried to change the DisplayItem template...errors should be shown when you are in edit mode, that is when the Edit template is on the screen, so 

Try to submit the form and see if the error is shown.  Let me know what happens.


Apr 1, 2012 at 10:49 PM

sorry, I put the wrong blog post. I meant to refer to

and I am using the MVC 2 example of the basic tutorial. Even, after submission the error messages don't show when editing an item but they show when adding an item.

Apr 2, 2012 at 7:40 AM

I was able to reproduce your issue! 

The point is that Mvc 2 uses the old Ajax Validation library that is not based on jQuery. It has several problems when dealing with data created dynamically.

The problem is caused by the fact that there are two error labels  with the same name(the labels created by the ValidationMessageFor helpers), one in the Display template and the other one in the Edit Template. This in some circumstances "confuses" the Microsoft validation system, that detects the error but is not able to set the error in the labels.

The Insert works, simply because the display Item associate to the insertion of a new ros is differen and DOESN'T CONTAIN the duplicate label.


Solution ?

Simple just remove the <%ValidationMessageFor(m => m.Name.....from the display template! It is totally uneusefull, since errors are shown just when the row is in edit mode! If you do this errors will be shown correctly.

However I advice to move to Mvc3....Mvc4 is on the door and it will be delivered in a short Mvc Controls Toolkit support for the Mvc 2 version will be dropped in about 1-2 years.

Apr 3, 2012 at 1:39 AM

There is one bug/feature I see with the JQuery validation and you can reproduce it using the aspx Mvc 3 example. This bug happens when you edit an items and you have validation errors. Then you click the undo/reset on the row you are editing. The row would go back to its original state but if you edit the exact same row again the exact same validation errors will be displayed even though they don't apply anymore. So, Is it a bug in the JQuery validation? or is something missing in the aspx Mvc 3 example?

By the way, you solution for the old Ajax Validation library worked perfectly. Thanks

Apr 3, 2012 at 9:39 AM

The one you found is a known bug, that was introduced by a re-engineering of the javascript code. It has been removed, and you will not have it in the new Mvc Controls Toolkit 2.0 version that should be released you need just to wait a day