I have the following property on my Model
[Display(Name = "MyProperty")]
[StringLength(10)]
public string MyProperty
{
get;set;
}
and the following EditorFor template
<%@ Page Language="C#" MasterPageFile="Template.Master" Inherits="System.Web.Mvc.ViewPage" %>
<asp:Content ID="Content1" ContentPlaceHolderID="Data" runat="server">
<%= Html.TextBox("", Model)%>
</asp:Content>
The problem I have is that the StringLength property (understandably), isn’t being set to limit the textbox size. My answer is how should I be obtaining the attributes to set in my template?
Thanks
There is a difference between metadata attributes and validation attributes.
StringLengthAttributeis a validation attribute, so you can’t get it fromModelMetadata.Luckily, Wayne Brantley has done the hard work. Here is how he gets the validation rules:
Note: if you are using ASP.NET MVC 3 or later, you will need to change
stringLengthtolengthandmaximumLengthtomax.