Html validationmessagefor not working

I have put together a small example here just to replicate the problem. I have a strongly typed partial view _Name.cshtml:

@model ValidationInPartial.ViewModels.MyViewModel <h2>@ViewBag.Message</h2> <fieldset> <legend>Name</legend> <div class=”editor-label”> @Html.LabelFor(model => model.MyName) </div> <div class=”editor-field”> @Html.EditorFor(model => model.MyName) @Html.ValidationMessageFor(model => model.MyName) </div> <a href=”#” id=”reload”>Reload Name</a> <p> <input type=”submit” value=”Create” /> </p> </fieldset> <script type=”text/javascript”> $(document).ready(function () { $(“#reload”).click(function () { $(“#divName”).load(“Home/NameReload”); }); }); </script>

that is initially loaded and displayed inside the main Index.cshtml

<div id=”divForm”> @using (Html.BeginForm()) { <div id=”divName”> @Html.Partial(“_Name”) </div> } </div>

The field MyName is required and validation is implemented through Required attribute in MyViewModel

namespace ValidationInPartial.ViewModels { public class MyViewModel { [Required(ErrorMessage = “Please enter a Name.”)] public string MyName { get; set; } } }

After the page is loaded the first time, if you click the Create button leaving the field empty the validation message “Please enter a Name.” shows beside the field and the field itself turns pink, which is the expected behaviour. Now by clicking the “Reload Name” link, which makes an ajax call (jquery.load(…)), the partial is reloaded, here is controller code:

public PartialViewResult NameReload() { MyViewModel myViewModel = new MyViewModel(); ViewBag.Message = “Name Reloaded”; return PartialView(“_Name”, myViewModel); }

This time if you click the Create button leaving the field empty the validation message does not appear beside the field, although the field turns pink. It turns out that when reloading the partial the @Html.ValidationMessageFor doesn’t render the validation message as the first time.

Here is the jquery files I use

<script src=”@Url.Content(“~/Scripts/jquery-1.5.1.min.js”)” type=”text/javascript”></script> <script src=”@Url.Content(“~/Scripts/jquery.validate.js”)” type=”text/javascript”></script> <script src=”@Url.Content(“~/Scripts/jquery.validate.unobtrusive.min.js”)” type=”text/javascript”></script> <script src=”@Url.Content(“~/Scripts/jquery.unobtrusive-ajax.min.js”)” type=”text/javascript”></script>

I wonder if this is a bug in the way the Razor engine renders the @Html.ValidationMessageFor or is that a problem with jquery? Any idea why this happens?

I have also read somewhere that the ajax call looses all the scripts for the page, in fact I have to keep any javascript code inside the partial so that they can be rendered and used again.

In the meantime I found a workaround which is to manually render in the partial what was supposed to be rendered by @Html.ValidationMessageFor which is:

<span class=”field-validation-valid” data-valmsg-replace=”true” data-valmsg-for=”MyName”></span>

However this workaround means that if we change the type of validation or just the validation message inside the Required attribute in the ViewModel, we need to modify this hard-coded piece of html in the view.

Related Posts