maxlength attribute of a text box from the DataAnnotations StringLength in Asp.Net MVC

前端 未结 7 697
攒了一身酷
攒了一身酷 2020-11-30 18:33

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 objec

7条回答
  •  萌比男神i
    2020-11-30 19:08

    I use the CustomModelMetaDataProvider to achieve this

    Step 1. Add New CustomModelMetadataProvider class

    public class CustomModelMetadataProvider : DataAnnotationsModelMetadataProvider
    {   
        protected override ModelMetadata CreateMetadata(
            IEnumerable attributes,
            Type containerType,
            Func modelAccessor,
            Type modelType,
            string propertyName)
        {
            ModelMetadata metadata = base.CreateMetadata(attributes,
                containerType,
                modelAccessor,
                modelType,
                propertyName);
    
            //Add MaximumLength to metadata.AdditionalValues collection
            var stringLengthAttribute = attributes.OfType().FirstOrDefault();
            if (stringLengthAttribute != null)
                metadata.AdditionalValues.Add("MaxLength", stringLengthAttribute.MaximumLength);
    
            return metadata;
        }
    }
    
    
    

    Step 2. In Global.asax Register the CustomModelMetadataProvider

    protected void Application_Start()
    {
        AreaRegistration.RegisterAllAreas();
        RegisterRoutes(RouteTable.Routes);
        ModelMetadataProviders.Current = new CustomModelMetadataProvider();
    }
    

    Step 3. In Views/Shared/EditorTemplates Add a partial view called String.ascx

    <%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl" %>
    <%if (!ViewData.ModelMetadata.AdditionalValues.ContainsKey("MaxLength")) { %>
        <%: Html.TextBox("", ViewData.TemplateInfo.FormattedModelValue,  new { @class = "text-box single-line" }) %>
    <% } else {
        int maxLength = (int)ViewData.ModelMetadata.AdditionalValues["MaxLength"];
        %>
        <%: Html.TextBox("", ViewData.TemplateInfo.FormattedModelValue, new { @class = "text-box single-line", MaxLength = maxLength  })%>
    <% } %>
    

    Done...

    Edit. The Step 3 can start to get ugly if you want to add more stuff to the textbox. If this is your case you can do the following:

    <%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl" %>
    <%
        IDictionary Attributes = new Dictionary();
        if (ViewData.ModelMetadata.AdditionalValues.ContainsKey("MaxLength")) {
            Attributes.Add("MaxLength", (int)ViewData.ModelMetadata.AdditionalValues["MaxLength"]);
        }
        if (ViewData.ContainsKey("style")) {
            Attributes.Add("style", (string)ViewData["style"]);
        }
        if (ViewData.ContainsKey("title")) {
            Attributes.Add("title", (string)ViewData["title"]);
        }
    %>
    <%: Html.TextBox("", ViewData.TemplateInfo.FormattedModelValue, Attributes)%>
    

    提交回复
    热议问题