有没有办法在编辑器模板之间重用自定义数据注释代码?

时间:2016-02-23 02:04:35

标签: asp.net-mvc data-annotations mvc-editor-templates

我有一些自定义数据注释可用于编辑ViewModels。

因为它们可以应用于任何类型的表单控件,所以我会在每个EditorTemplate中对它们进行测试。

有人可以推荐一种在MVC中重用这样的共享视图代码的方法吗?

我考虑使用HtmlHelper或AppCode / Helper类。不确定哪一个最好,如果两者都是新的。

@{
    var htmlAttributesFromView = ViewData["htmlAttributes"] ?? new { };
    var htmlAttributes = Html.MergeHtmlAttributes(htmlAttributesFromView, new { @class = "form-control" });


    bool isDisplayInfoOnIconClickAttribute = false;
    string title = "";
    string description = "";

    var infoOnClickAttributes = (ViewData.ModelMetadata).ContainerType.GetProperty(ViewData.ModelMetadata.PropertyName).GetCustomAttributes(typeof(DisplayInfoOnIconClickAttribute), false);
    if (infoOnClickAttributes.Length > 0)
    {
        DisplayInfoOnIconClickAttribute attribute = infoOnClickAttributes[0] as DisplayInfoOnIconClickAttribute;
        isDisplayInfoOnIconClickAttribute = true;
        title = attribute.Title;
        description = attribute.Description;
    }


    bool isDisplayTextInfoAttribute = false;
    string info = "";

    var textInfoAttributes = (ViewData.ModelMetadata).ContainerType.GetProperty(ViewData.ModelMetadata.PropertyName).GetCustomAttributes(typeof(DisplayTextInfoAttribute), false);
    if (textInfoAttributes.Length > 0)
    {
        DisplayTextInfoAttribute attribute = textInfoAttributes[0] as DisplayTextInfoAttribute;
        isDisplayTextInfoAttribute = true;
        info = attribute.Info;
    }
}



<div class="form-group">
    @Html.LabelFor(model => model, htmlAttributes: new { @class = "control-label col-md-3 text-right-md" })
    <div class="col-md-8">

        @Html.TextBoxFor(model => model, htmlAttributes)
        @Html.ValidationMessageFor(model => model)
    </div>
    <a class="infoonclick col-md-1" title="@Html.DisplayNameFor(model => model)" data-content="@Html.DescriptionFor(model => model)">
        <span class="fa fa-info-circle"></span>
    </a>
</div>

1 个答案:

答案 0 :(得分:0)

感谢斯蒂芬斯的建议,我已经整理了修订后的代码主张。

我避免使用HtmlHelper因为这个要求似乎有点过分了。

<强>模型

public class Car : DbEntity, IDbEntity
{
    public virtual string Model { get; set; }

    [DisplayTextInfo("E.g. pink, yellow or green. For more colours please <a href='http://hslpicker.com/'>click here</a>.")]
    public virtual string Colour { get; set; }
}   

数据注释

public class DisplayTextInfoAttribute : Attribute, IMetadataAware
{
    public DisplayTextInfoAttribute(string info)
    {
        Info = info;
    }

    public void OnMetadataCreated(ModelMetadata metadata)
    {
        if (Info != null)
        {
            metadata.AdditionalValues["DisplayTextInfo"] = Info;
        }
    }
    public string Info { get; set; }
}

编辑模板

<div class="form-group">
    @Html.LabelFor(model => model)

    @Html.TextBoxFor(model => model, htmlAttributes)
    @Html.ValidationMessageFor(model => model)

    @if (ViewData.ModelMetadata.AdditionalValues.ContainsKey("DisplayTextInfo"))
    {
        <p class="help-block">@Html.Raw(ViewData.ModelMetadata.AdditionalValues["DisplayTextInfo"].ToString())</p>
    }
</div>