ASPNET MVC - 用相同签名的新帮手覆盖Html.TextBoxFor(model.property)? [英] ASPNET MVC - Override Html.TextBoxFor(model.property) with a new helper with same signature?

查看:194
本文介绍了ASPNET MVC - 用相同签名的新帮手覆盖Html.TextBoxFor(model.property)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用我自己的助手具有完全相同的签名(当然不同的命名空间)来覆盖Html.TextBoxFor() - 这是可能的,如果是这样,如何​​

I want to override Html.TextBoxFor() with my own helper that has the exact same signature (but a different namespace of course) - is this possible, and if so, how?

这样做的原因是,我在一个已经存在的应用程序100+的意见,我想改变TextBoxFor的行为,使其输出最大长度,如果酒店有[StringLength(N)]注释= n属性

The reason for this is that I have 100+ views in an already existing app, and I want to change the behaviour of TextBoxFor so that it outputs a maxLength=n attribute if the property has a [StringLength(n)] annotation.

在code自动输出最大长度= n是在这个问题:<一href=\"http://stackoverflow.com/questions/2386365/maxlength-attribute-of-a-text-box-from-the-dataannotations-stringlength-in-mvc2\">http://stackoverflow.com/questions/2386365/maxlength-attribute-of-a-text-box-from-the-dataannotations-stringlength-in-mvc2.但我的问题是不是重复 - 我想创造一个更通用的解决方案:其中DataAnnotaion无需任何通过写观点的人额外的code自动流入HTML

The code for automatically outputting maxlength=n is in this question: http://stackoverflow.com/questions/2386365/maxlength-attribute-of-a-text-box-from-the-dataannotations-stringlength-in-mvc2. But my question is not a duplicate - I am trying creating a more generic solution: where the DataAnnotaion flows into the html automatically without any need for additional code by the person writing the view.

在引用的问题,你必须每一个Html.TexBoxFor更改为Html.CustomTextBoxFor。我需要做的是使现有的TextBoxFor()的并不需要改变 - 因此创建具有相同签名的助手:改变助手方法的行为,以及所有现有实例只是没有任何变化工作(100 +观点,至少500 TextBoxFor()秒 - 不想手动编辑)。

In the referenced question, you have to change every single Html.TexBoxFor to a Html.CustomTextBoxFor. I need to do it so that the existing TextBoxFor()'s do not need to be changed - hence creating a helper with the same signature: change the behaviour of the helper method, and all existing instances will just work without any changes (100+ views, at least 500 TextBoxFor()s - don't want to manually edit that).

我想这code:(我需要重复它TextBoxFor每过载,但一旦根本问题得到解决,这将是微不足道的)

I tried this code: (And I need to repeat it for each overload of TextBoxFor, but once the root problem is solved, that will be trivial)

namespace My.Helpers
{
    public static class CustomTextBoxHelper
    {
        public static MvcHtmlString TextBoxFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression, object htmlAttributes, bool includeLengthIfAnnotated)
        {
                // implementation here
        }
    }
}

但我在Html.TextBoxFor的观点得到一个编译器错误()说​​:调用以下方法或属性之间暧昧(当然)。有没有办法做到这一点?

But I am getting a compiler error in the view on Html.TextBoxFor(): "The call is ambiguous between the following methods or properties" (of course). Is there any way to do this?

是否有其他办法,使我改变Html.TextBoxFor的行为,让已经用它的观点并不需要改变?

Is there an alternative approach that would allow me to change the behaviour of Html.TextBoxFor, so that the views that already use it do not need to be changed?

推荐答案

您不能有相同的名称,并在同一时间同一签名的两个扩展方法。你可以把你的扩展方法为自定义命名空间,并在你的web.config使用此命名空间,而不是默认的(System.Web.Mvc.Html):

You cannot have two extension methods with the same name and the same signature at the same time. You could put your extension method into a custom namespace and use this namespace instead of the default one (System.Web.Mvc.Html) in your web.config:

<pages>
    <namespaces>
        <!--<add namespace="System.Web.Mvc.Html"/>-->
        <add namespace="YourCompany.Web.Mvc.Html"/>
    </namespaces>
</pages>

但如果你这样做,你将失去所有其他的推广方法,你将需要重写他们在您的自定义命名空间。

but if you do this you will lose all the other extension methods and you will need to override them in your custom namespace.

这篇关于ASPNET MVC - 用相同签名的新帮手覆盖Html.TextBoxFor(model.property)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆