Partial view without validation attributes (ASP.NET MVC 3)

The strange thing continues to happen in one of my ASP.NET MVC 3 applications.

I am extracting the insertion strings through jQuery Ajax api and there is no problem with that. But when I get the necessary partial view back, it arrives without validation attributes, and I cannot repeat the validation for these lines.

Here is what I get as ajax answer:

<input type="hidden" name="accommPropertyPeriods.index" autocomplete="off" value="ccaa15b3-76f1-4215-8bb5-a62d700bfc1e" /> <table style="width:100%;"> <tr> <td> <div class="editor-field"> <select class="chzn-select-deselect" data-placeholder="Choose an Alias..." id="accommPropertyPeriods_ccaa15b3-76f1-4215-8bb5-a62d700bfc1e__AccommPropertySeasonPeriodAliasID" name="accommPropertyPeriods[ccaa15b3-76f1-4215-8bb5-a62d700bfc1e].AccommPropertySeasonPeriodAliasID" style="min-width:100px;"><option value="302">A</option> <option value="303">B</option> <option value="304">C</option> <option value="305">D</option> </select> </div> </td> <td> <div class="editor-field"> <input class="datefield" id="accommPropertyPeriods_ccaa15b3-76f1-4215-8bb5-a62d700bfc1e__PeriodStartsAt" name="accommPropertyPeriods[ccaa15b3-76f1-4215-8bb5-a62d700bfc1e].PeriodStartsAt" type="text" value="" /> </div> </td> <td> <div class="editor-field"> <input class="datefield" id="accommPropertyPeriods_ccaa15b3-76f1-4215-8bb5-a62d700bfc1e__PeriodEndsAt" name="accommPropertyPeriods[ccaa15b3-76f1-4215-8bb5-a62d700bfc1e].PeriodEndsAt" type="text" value="" /> </div> </td> </tr> </table> 

Here is what I should get:

 <input type="hidden" name="accommPropertyPeriods.index" autocomplete="off" value="84ddd0f5-a3e2-4f10-8e67-f32528c6393d" /> <table style="width:100%;"> <tr> <td> <div class="editor-field"> <select class="chzn-select-deselect" data-placeholder="Choose an Alias..." data-val="true" data-val-number="The field AccommPropertySeasonPeriodAliasID must be a number." data-val-required="The AccommPropertySeasonPeriodAliasID field is required." id="accommPropertyPeriods_84ddd0f5-a3e2-4f10-8e67-f32528c6393d__AccommPropertySeasonPeriodAliasID" name="accommPropertyPeriods[84ddd0f5-a3e2-4f10-8e67-f32528c6393d].AccommPropertySeasonPeriodAliasID" style="min-width:100px;"><option value="302">A</option> <option value="303">B</option> <option value="304">C</option> <option value="305">D</option> </select> <span class="field-validation-valid" data-valmsg-for="accommPropertyPeriods[84ddd0f5-a3e2-4f10-8e67-f32528c6393d].AccommPropertySeasonPeriodAliasID" data-valmsg-replace="false">*</span> </div> </td> <td> <div class="editor-field"> <input class="datefield" data-val="true" data-val-required="The PeriodStartsAt field is required." id="accommPropertyPeriods_84ddd0f5-a3e2-4f10-8e67-f32528c6393d__PeriodStartsAt" name="accommPropertyPeriods[84ddd0f5-a3e2-4f10-8e67-f32528c6393d].PeriodStartsAt" type="text" value="" /> <span class="field-validation-valid" data-valmsg-for="accommPropertyPeriods[84ddd0f5-a3e2-4f10-8e67-f32528c6393d].PeriodEndsAt" data-valmsg-replace="false">*</span> </div> </td> <td> <div class="editor-field"> <input class="datefield" data-val="true" data-val-required="The PeriodEndsAt field is required." id="accommPropertyPeriods_84ddd0f5-a3e2-4f10-8e67-f32528c6393d__PeriodEndsAt" name="accommPropertyPeriods[84ddd0f5-a3e2-4f10-8e67-f32528c6393d].PeriodEndsAt" type="text" value="" /> <span class="field-validation-valid" data-valmsg-for="accommPropertyPeriods[84ddd0f5-a3e2-4f10-8e67-f32528c6393d].PeriodEndsAt" data-valmsg-replace="false">*</span> </div> </td> </tr> </table> 

GUIDs do not have to be the same. I do the so-called non-sequential binding.

Here is the action I call through jquery ajax to get a new insert line:

  [HttpPost] public PartialViewResult accommPropertySeasonPeriodCreatePartialView(int id, int subid) { //some other stuff going on here. non-related to partial view. return PartialView("_AccommPropertySeasonPeriodCreatePartialView"); } 

I hardly understand why this is happening. Any idea?

+6
source share
1 answer

Html.* Helpers such as Html.TextBoxFor , Html.CheckBoxFor , ... emit validation attributes only if they are used inside the form. Therefore, be sure to wrap them in an Html.BeginForm call. As for client-side validation, you should call the jQuery.validator.unobtrusive.parse method after updating your DOM to re-apply client validation. And one more article .

If you do not have a form, you can cheat and put in partial the following:

 @model MyViewModel @{ ViewContext.FormContext = new FormContext(); } @Html.EditorFor(x => x.Foo) 

Now the assistant will display the data validation attributes * * in the input field.

+10
source

Source: https://habr.com/ru/post/901514/


All Articles