Quantcast
Channel: Developer to developer
Viewing all articles
Browse latest Browse all 9076

UIHint and DataAnnotations

$
0
0

I am trying to utilize data annotations and uihints to creat forms for my site.  I am placing my data annotations on the properties of the view model for validation purposes and they are working just fine when I render out the code in the Index.cshtml file as follows:

@Html.PropertyFor(m => m.Value)

I am also creating a custom UIHint display template to create our input boxes in the same fashion each time rather than copy the code every time in the view.  So my property looks like this.

[Required]
[UIHint("TextboxForm"]
public string FirstName {get;set;}

The issue seems to be that when using the Display Template for TextboxForm, it is not rendering out the data attributes for unobtrusive javascript correctly.  In this case, I am getting the following rendering of html.

<div class="form-group">
<label class=" sr-only control-label" for="Form_LastName">Last Name*</label>
<input class="form-control" id="Form_LastName" name="Form.LastName" placeholder="Last Name*" type="text" value="">
<span class="field-validation-valid" data-valmsg-for="Form.LastName" data-valmsg-replace="true"></span>
</div>

The following is what I would expect to get it out if I didn't use the Html.PropertyFor and instead just did @Html.LabelFor and @Html.TextboxFor

<div class="form-group"><label class=" sr-only control-label" for="Form_FirstName">First Name</label><input class="form-control" data-val="true" data-val-required="The First Name field is required." id="Form_FirstName" name="Form.FirstName" type="text" value=""><span class="field-validation-valid" data-valmsg-for="Form.FirstName" data-valmsg-replace="true"></span></div>

Can someone provide any insight as to what could be done to pass the data annotations to the DisplayTemplate properly?


Viewing all articles
Browse latest Browse all 9076

Trending Articles