How to read property data annotation value in .NET MVC

前端 未结 6 1555
独厮守ぢ
独厮守ぢ 2020-12-17 04:43

I Just starting out w/ ASP.NET MVC 3 and I am trying to render out the following HTML for the string properties on a ViewModel on the create/edit view.



        
6条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-17 05:37

    Instead of the StringLength attribute (because it's a validator attribute not a metadata provider) you can use the AdditionalMetadata attribute. Sample usage:

    public class ViewModel
    {
        [AdditionalMetadata("maxLength", 30)]
        public string Property { get; set; }
    }
    

    Basically it puts the value 30 under the key maxLength in the ViewData.ModelMetadata.AdditionalValues dictionary. So you can use it your EditorTemplate:

    
    

提交回复
热议问题