Custom HTML attributes in MVC3 Model

I want to create a Model: public class TestModel { Microdata(Data = "data-test=this is a test!")] public bool Test { get; set; } } Then in the view: @Html.DisplayForModel() The result i'm looking for is something like this: I've already created a custom attribute class, but this didn't produce anything. [AttributeUsage(AttributeTargets.Property | AttributeTargets.Class, AllowMultiple = false, Inherited = true)] public class MicrodataAttribute : Attribute { public string Data { get; set; } public RouteValueDictionary GetAttributes() { var attributes = new RouteValueDictionary(); if (this.Data != null) { string[] kv = this.Data.Split(','); attributes.Add(kv[0], kv[1]); } return attributes; } } public class MetadataProvider : DataAnnotationsModelMetadataProvider { protected override ModelMetadata CreateMetadata(IEnumerable attributes, Type containerType, Func modelAccessor, Type modelType, string propertyName) { var metadata = base.CreateMetadata(attributes, containerType, modelAccessor, modelType, propertyName); var additionalValues = attributes.OfType().FirstOrDefault(); if (additionalValues != null) { metadata.AdditionalValues.Add("HtmlAttributes", additionalValues); } return metadata; } }

以上就是Custom HTML attributes in MVC3 Model的详细内容,更多请关注web前端其它相关文章!

赞(0) 打赏
未经允许不得转载:web前端首页 » HTML5 答疑

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

前端开发相关广告投放 更专业 更精准

联系我们

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏