1

Moving from HtmlHelpers I created a few year ago, I have a custom Taghelper that as is marked up during development as so...

    <gdropdown asp-for="Type_ID" asp-items="cboType_SelectOne"></gdropdown>

All works fine and the output for this is as follows...

    <div>
        <select id="Type_ID" name="Type_ID">
            <option value=-1> - Select One - </option>
            <option value=9>Aux. Ext. Device</option>
            <option value=28>Backup Device</option>
            ...
        </select>
    </div>

Now I am looking for help crafting my OWN validation SPAN to add to the output as follows...

    <div>
        <select id="Type_ID" name="Type_ID">
            <option value=-1> - Select One - </option>
            <option value=9>Aux. Ext. Device</option>
            <option value=28>Backup Device</option>
            ...
        </select>

        <span class="field-validation-valid" data-valmsg-replace="true" data-valmsg-for="Type_ID">
            <span for="Type_ID">Req!</span>
        </span>
    </div>

Anyway, I can manage to do the basics of creating that SPAN, however I can't get to the data annotations for the field that is being added/edited.

Using HtmlHelpers I could use htmlHelper.ValidationMessageFor() as follows in order to craft the output automatically...

    public static MvcHtmlString CustomLookupFor2<TModel, TValue>(this HtmlHelper<TModel> htmlHelper,
        Expression<Func<TModel, TValue>> exp, string url, bool includeValidation, object options)
    {
        ...
        if (includeValidation) sbCtrls.Append(htmlHelper.ValidationMessageFor(expression));
        ...
    }

From within the TagHelper ProcessAsync method, how can I get to the same or similar data annotation information? Or better still, how can I auto generate the validation element entirely in in a similar fashion as above?

4

1 回答 1

1

**** 解决方案 - 2016 年 3 月 13 日 ****

您可以使用ModelExpression通过asp-for属性传入的。更具体地说是modExp.Metadata.ValidatorMetadata类。

我发现旧的验证注释方法不适用于 EF7。至少不是我习惯的时尚。

    [MetadataType(typeof(IT_AssetValidation))]
    public partial class IT_Asset
    {
        public class IT_AssetValidation
        {
            [Range(1, 1000000, ErrorMessage = "Req!")]
            public string Product_ID { get; set; }

            [Required(ErrorMessage = "Req!")]
            public string Date_Acquired { get; set; }
        }
    }

目前,在 EF7 支持其他数据注释之前,我必须直接在我的 POCO 类中创建我的注释(目前仅用于开发/测试目的),如下所示。鉴于我只使用一张表来帮助创建我的新 TagHelpers,这没什么大不了的。仅允许我继续开发这些 TagHelper,为我最终过渡到 MVC 6 做好准备,一旦 EF7 功能齐全并稍微稳定下来。

    public partial class IT_Asset
    {
        public int Asset_ID { get; set; }

        [Required(ErrorMessage = "Req!")]
        public DateTime? Date_Acquired { get; set; }

        [Range(1, 1000000, ErrorMessage = "Req!")]
        public int? Product_ID { get; set; }
    }

现在创建我的验证的代码<span data-...></span>如下所示。

    public static TagBuilder ValidationTag(ModelExpression modExp)
    {
        if ((modExp != null) && (modExp.Metadata.ValidatorMetadata.Count > 0))
        {
            //Construct the HTML span template
            var valTag = new TagBuilder("span");
            valTag.Attributes.Add("class", "");
            valTag.Attributes.Add("data-valmsg-for", modExp.Name);

            return valTag;
        }
        else return null;
    }

为正在验证的标签派生验证属性的代码如下。这是刚刚的早期伪代码。我可能会稍后更新。

    public static string ValidationAttributes(ModelExpression modExp)
    {
        if ((modExp != null) && (modExp.Metadata.ValidatorMetadata.Count > 0))
        {
            var ruleAttributes = new StringBuilder();

            //*PSUEDO CODE* Return the rules 
            foreach(...) {
                 If (REQ) ruleAttributes.Append(REQ attribute)
                 If (RANGE) ruleAttributes.Append(RANGE attribute)
                 etc. etc.
            }

            return ruleAttributes.ToString();
        }
        else return "";
    }

如果有人知道创建验证标签和/或为正在验证的标签派生属性的更自动的方法,而不是我在这里的手动方法,我将非常感激。

随着我的代码和理解的发展,我将更新此解决方案。

于 2016-04-13T09:22:45.677 回答