I am working on an MVC2 application and want to set the maxlength attributes of the text inputs.
I have already defined the stringlength attribute on the Model object using data annotations and it is validating the length of entered strings correctly.
I do not want to repeat the same setting in my views by setting the max length attribute manually when the model already has the information. Is there any way to do this?
Code snippets below:
From the Model:
[Required, StringLength(50)]
public string Address1 { get; set; }
From the View:
<%= Html.LabelFor(model => model.Address1) %>
<%= Html.TextBoxFor(model => model.Address1, new { @class = "text long" })%>
<%= Html.ValidationMessageFor(model => model.Address1) %>
What I want to avoid doing is:
<%= Html.TextBoxFor(model => model.Address1, new { @class = "text long", maxlength="50" })%>
I want to get this output:
<input type="text" name="Address1" maxlength="50" class="text long"/>
Is there any way to do this?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…