When using any of the Input Extension Helper Methods, like @Html.TextboxFor
, any Validation Attributes from your model are automatically generated by the Razor engine (via ClientValidationEnabled
/UnobtrusiveJavaScriptEnabled
).
For example, take the following case which works fine
Model:
[Required]
public string QuestionOne { get; set; }
View:
@Html.TextBoxFor(model => model.QuestionOne)
@Html.ValidationMessageFor(model => model.QuestionOne)
Generated Markup:
<input type="text" id="QuestionOne" name="QuestionOne" value=""
data-val="true" data-val-required="The QuestionOne field is required." >
<span class="field-validation-valid" data-valmsg-for="QuestionOne" data-valmsg-replace="true"></span>
In this case the attributes data-val="true"
& data-val-required="The QuestionOne field is required."
are picked up by Unobtrusive validation and the form element is successfully validated.
However, for extensibility reasons, I want to be able to generate the <input>
element myself instead of using TextBoxFor
. So my view would now look like this:
<input type="textbox"
id="@Html.IdFor(m => m.QuestionTwo)"
name="@Html.NameFor(m => m.QuestionTwo)"
value="@Model.QuestionTwo"
data-val="true" data-val-required="Selection is Required" />
@Html.ValidationMessageFor(model => model.QuestionTwo)
In this case, I'm faking the validation attribute output by just re-writing data-val="true" (etc)
by hand, but this would have to be expanded to cover every single case.
Here's a running Demo in .NET Fiddle
Q: Can I build /return a list of data-val-*
attributes for a given element?
HtmlHelper
has aGetUnobtrusiveValidationAttributes()
method that returns thedata-*
attributes (but you need to get theModelMetaData
from the expression). All you have shown is the exact same html that will be generated by theTextBoxFor()
method except for thevalue
attribute if the property is invalid (because your no longer binding correctly). What are you wanting to achieve with this? – Glooming<select>
element and to this<input typed="checked">
element. – OnymModelMetadata
andHtmlHelper
methods, and have complete control over your html (and in that example, the view code would be (say)@Html.GeoDropdownListFor(m=> m.CityId, Model.Cities);
– GloomingGetUnobtrusiveValidationAttributes()
method? Or are you not understanding how to use it? – GloomingGetUnobtrusiveValidationAttributes
, in that the first time I call the method I'll get real results, but immediately refreshing it turns up nothing, which I suspect is creating a problem when calling it from inside an EditorTemplate. I'll write up separately what in the world I'm trying to do; just been trying to keep questions on topic and narrow in scope. – OnymOn the first call, a collection of attributes is returned. Further calls (on the same field) return an empty collection
- I wonder if that's what's clearing the collection when I try to useHtml.GetUnobtrusiveValidationAttributes(Html.NameFor(model => model).ToString())
inside of anEditorFor()
if during the route there, it has already called that method behind the scenes – Onymdata-val-*
attributes will not be added for the 2nd one (I'll find a link for you shortly to explain). Just refreshing the page will not result in the attributes 'disappearing'. If they are its something else in your code causing the issue – Glooming