user683302
user683302

Reputation: 135

validationmessagefor missing "data-valmsg-for" in editorfor

I have a collection of objects that I'm trying to display, however the span generated by the ValidationMessageFor does not include all the validation attributes:

<span class="field-validation-error">This field is required</span>

instead of:

<span class="field-validation-error" data-valmsg-replace="true" data-valmsg-for="Questions[0].SingleAnswer"></span>

This is how I'm generating the html:

<fieldset id="dr_profileUpdates">
       @Html.EditorFor(model => model.Questions)
</fieldset>

And here is my editor template:

@Html.ValidationMessageFor(model => model.SingleAnswer)
@Html.TextBoxFor(model => model.SingleAnswer, new { @class = "textBoxDefault" })

The validation works, however the span does not dissapear after filling out the textbox and focusing out of it - I would assume this is because the span does not get generated correctly

Any help is appreciated.

EDIT: Actually it appears that after posting back to the server and returning the partial view (assuming the ModelState is invalid), those attributes do not get generated again - it only seems to affect the ValidationMessage. Any ideas?

Thanks,

Upvotes: 3

Views: 7153

Answers (1)

Pedro Ad&#227;o
Pedro Ad&#227;o

Reputation: 36

Try add on top of your razor file

@{
Html.EnableUnobtrusiveJavaScript();
}

Upvotes: 1

Related Questions