如何:嵌套扩展方法调用

时间:2011-10-27 14:10:02

标签: c# asp.net-mvc asp.net-mvc-3

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

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

因为字段'required'已经被定义为数据注释(为了简单和干净的JS验证而添加了属性),我想编写一个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 我找到了问题的原因,但还不是一个干净的解决方案。问题是在GetTextBoxFor函数中调用的GetUnobtrusiveValidationAttributes函数。 这不会到达我的对象上的数据注释,因为调用函数时使用的名称应该有[CLASS]。[PROPERTY]结构,而ExpressionHelper.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));
    }

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

1 个答案:

答案 0 :(得分:0)

在我的帮助程序类中,当我添加数据然后传递给基础MVC 3帮助程序时,我使用以下内容:

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

我没有检查此问题的MVC 3源代码,但我希望通过htmlHelper进行调用是导致问题的原因。