如何覆盖HTMLTags LabelFor

时间:2016-04-22 14:21:46

标签: .net asp.net-mvc-5

使用HTMLTagsJimmyMVC conventions我在视图中有以下内容:

@Html.FormBlock(aModel => aModel.PostQuery.ProjectId)

但标签生成为" PostQueryProjectId"

如何覆盖生成器才能返回" ProjectId"?

2 个答案:

答案 0 :(得分:1)

在PostQuery模型中添加注释

class PostQuery
{
[Display(Name="Post Query")]
public int ProjectId {get; set; }
...
}

答案 1 :(得分:1)

史蒂芬,

我认为您可以通过实现自己的HtmlConventionRegistry来覆盖约定

public class DefaultAspNetMvcHtmlConventions : HtmlConventionRegistry
{
    public DefaultAspNetMvcHtmlConventions()
    {
        Labels.Always.ModifyWith(er => er.CurrentTag.Text(er.Accessor.Name));
    }
}

然后在将注册表添加到IoC容器之前应用您的注册表(这是我的结构图)

public class HtmlTagRegistry : Registry
{
    public HtmlTagRegistry()
    {
        var htmlConventionLibrary = new HtmlConventionLibrary();
        new DefaultHtmlConventions().Apply(htmlConventionLibrary);
        new DefaultAspNetMvcHtmlConventions().Apply(htmlConventionLibrary);
        For<HtmlConventionLibrary>().Use(htmlConventionLibrary);
    }
}