Note that there are some explanatory texts on larger screens.

plurals
  1. POMVC3 Razor Partial view render does not include data- validation attributes
    primarykey
    data
    text
    <p>I have a farily straight forward form that renders personal data as a partial view in the center of the form. I can not get client side validation to work on this form. I started chasing down the generate html and came up with the same model field rendered on a standard form and a partial view.</p> <p>I noticed that the input elements are correctly populated on the first call, @html.partial, the following only happens when the partialview is reloaded via an ajax request.</p> <p>First the header of my partial view, this is within a Ajax.BeginForm on the main page.</p> <pre><code>@model MvcMPAPool.ViewModels.EventRegistration &lt;script src="@Url.Content("~/Scripts/jquery.validate.js")" type="text/javascript"&gt;&lt;/script&gt; &lt;script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.js")" type="text/javascript"&gt;&lt;/script&gt; &lt;script type="text/javascript"&gt; $(document).ready(function () { $(".phoneMask").mask("(999) 999-9999"); }); &lt;/script&gt; @{ var nPhn = 0; var dTotal = 0.0D; var ajaxOpts = new AjaxOptions{ HttpMethod="Post", UpdateTargetId="idRegistrationSummary", OnSuccess="PostOnSuccess" }; Html.EnableClientValidation( true ); Html.EnableUnobtrusiveJavaScript( true ); } </code></pre> <p>Here is the razor markup from the partial view:</p> <pre><code>@Html.ValidationMessageFor(model=&gt;Model.Player.Person.Addresses[0].PostalCode) &lt;table&gt; &lt;tr&gt; &lt;td style="width:200px;"&gt;City*&lt;/td&gt; &lt;td&gt;State&lt;/td&gt; &lt;td&gt;Zip/Postal Code&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;@Html.TextBoxFor(p=&gt;Model.Player.Person.Addresses[0].CityName, new { style="width:200px;", maxlength=50 })&lt;/td&gt; &lt;td&gt; @Html.DropDownListFor(p=&gt; Model.Player.Person.Addresses[0].StateCode , MPAUtils.GetStateList(Model.Player.Person.Addresses[0].StateCode))&lt;/td&gt; &lt;td&gt; &lt;div class="editor-field"&gt; @Html.TextBoxFor(p=&gt;Model.Player.Person.Addresses[0].PostalCode, new { style="width:80px;", maxlength=10 }) &lt;/div&gt; &lt;/td&gt; &lt;/tr&gt; &lt;/table&gt; </code></pre> <p>Here is the rendered field from the partial view:</p> <pre><code> &lt;td&gt; &lt;div class="editor-field"&gt; &lt;input id="Player_Person_Addresses_0__PostalCode" maxlength="10" name="Player.Person.Addresses[0].PostalCode" style="width:80px;" type="text" value="" /&gt; &lt;/div&gt; &lt;/td&gt; </code></pre> <p>Here is the same model field rendered in a standard view:</p> <pre><code> &lt;div class="editor-field"&gt; &lt;input data-val="true" data-val-length="The field Postal/Zip Code must be a string with a maximum length of 10." data-val-length-max="10" data-val-required="Postal or Zip code must be provided!" id="Person_Addresses_0__PostalCode" maxlength="10" name="Person.Addresses[0].PostalCode" title="Postal/Zip Code is required" type="text" value="" /&gt; &lt;span class="field-validation-valid" data-valmsg-for="Person.Addresses[0].PostalCode" data-valmsg-replace="true"&gt;&lt;/span&gt; &lt;/div&gt; </code></pre> <p>Notice that the partial view rendering has no data-val-xxx attributes on the input element.</p> <p>Is this correct? I do not see how the client side validation could work without these attributes, or am I missing something basic here?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload