如何:嵌套扩展方法调用

本文关键字:方法 调用 扩展 嵌套 如何 | 更新日期: 2023-09-27 18:12:46

在我的cshtml中,我有以下行:

@Html.TextBoxFor(m => m.EmailAddress, new { @class = "field size4",
placeholder = LogOnUIMessages.EmailFieldLabel, id = "LoginEmailAddress",
name="LoginEmail", autofocus = "", required=""})

因为字段'required'已经被定义为数据注释(添加属性是为了方便&我想写一个新的TextBoxFor实现,它包含了基于数据注释的"required":

[Required(ErrorMessageResourceType = typeof(LogOnUIMessages), ErrorMessageResourceName = LogOnResourceKeys.ErrorEmailIsRequired)]
[StringLength(UserNameMaxLength, ErrorMessageResourceType = typeof(LogOnUIMessages), ErrorMessageResourceName = LogOnResourceKeys.ErrorUserNameMaxLenghtExceeded)]
[RegularExpression(RegEx.CorrectEmailRegExp, ErrorMessageResourceType = typeof(ProfileUIMessages), ErrorMessageResourceName = LogOnResourceKeys.ErrorEmailIsNotValid)]
[DataType(DataType.EmailAddress)]
public String EmailAddress { get; set; }

(有更多的注释给出,现在只显示最重要的一个)作为概念证明,我写了一个扩展方法和一个辅助方法:

public static MvcHtmlString CustomTextBoxFor<TModel, TProperty>(
        this HtmlHelper<TModel> htmlHelper,
        Expression<Func<TModel, TProperty>> expression,
        object htmlAttributes
    )
{
    return htmlHelper.TextBoxFor(expression, htmlAttributes);
}

public static MvcHtmlString CustomTextBoxForToo<TModel, TProperty>(
        HtmlHelper<TModel> htmlHelper,
        Expression<Func<TModel, TProperty>> expression,
        object htmlAttributes
    )
{
    return htmlHelper.TextBoxFor(expression, 
    HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes));
}

但是当像

这样调用时
@Html.CustomTextBoxFor(
    m => m.EmailAddress, new { @class = "field size4", placeholder = 
    LogOnUIMessages.EmailFieldLabel, id = "LoginEmailAddress",
    name="LoginEmail", autofocus = "", required=""})

@PocExtensions.CustomTextBoxForToo(
    this.Html, m => m.EmailAddress, new { @class = "field size4", 
    placeholder = LogOnUIMessages.EmailFieldLabel, 
    id = "LoginEmailAddress", name="LoginEmail", 
    autofocus = "", required=""})

它们都返回:

<input autofocus="" class="field size4" id="LoginEmailAddress" 
name="EmailAddress" placeholder="vul je e-mail adres in" 
required="" type="text" value="">

而不是我所期望的:

<input autofocus="" class="field size4" data-val="true" 
data-val-length="Het opgegeven emailadres is te lang." 
data-val-length-max="128" data-val-regex="Het opgegeven adres is niet geldig" 
data-val-regex-pattern="[A-Za-z0-9!#$%&amp;'*+/=?^_`{|}~-]+(?:'.[A-Za-z0-
9!#$%&amp;'*+/=?^_`{|}~-]+)*@(?:[A-Za-z0-9](?:[A-Za-z0-9-]*[A-Za-z0-9])
?'.)+[A-Za-z0-9](?:[A-Za-z0-9-]*[A-Za-z0-9])?" 
data-val-required="Dit veld is verplicht." id="LoginEmailAddress" 
name="EmailAddress" placeholder="vul je e-mail adres in" 
required="" type="text" value="">

我做错了什么?

编辑1,附加信息

IEnumerable<ModelValidator> modelValidators=metadata.GetValidators(htmlHelper.ViewContext); 

返回4个验证器。:

htmlHelper.GetUnobtrusiveValidationAttributes(elementName, metadata);

返回一个空集合?

编辑# 2 我找到了问题的原因,但还没有找到彻底的解决办法。问题是GetUnobtrusiveValidationAttributes函数,该函数在GetTextBoxFor函数中被调用。这没有到达我的对象上的数据注释,因为调用函数时使用的名称应该有一个[CLASS]。[PROPERTY]结构。GetExpressionText函数(在GetTextBoxFor中使用)只产生[PROPERTY]名称。

现在我想知道,这是一个错误还是设计?

我已经设法把一个POC:

public static MvcHtmlString CustomTextBoxFor<TModel, TProperty>(
    this HtmlHelper<TModel> htmlHelper,
    Expression<Func<TModel, TProperty>> expression,
    object htmlAttributes
)
{
    var elementName = ExpressionHelper.GetExpressionText(expression);
    var metadata = ModelMetadata.FromLambdaExpression(expression, htmlHelper.ViewData);
    var dataAnnotationAttributeDictionary = htmlHelper.GetUnobtrusiveValidationAttributes
(
    String.Format("{0}.{1}", metadata.ContainerType.FullName, elementName),
    metadata
);
var providedAttributeDictionary = HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes);
return htmlHelper.TextBoxFor(expression, providedAttributeDictionary.Concat(dataAnnotationAttributeDictionary).ToDictionary(pair => pair.Key, pair => pair.Value));
    }

现在我的问题是:(如何)这可以做得更好?

如何:嵌套扩展方法调用

在我的helper类中,当我添加数据然后传递到底层MVC 3 helper时,我使用以下内容:

return InputExtensions.TextBoxFor(htmlHelper, expression, htmlAttributes);

我没有检查到这个问题的MVC 3源代码,但我希望通过htmlHelper调用是您的问题的原因。