ASPNET MVC - 使用具有相同签名的新助手覆盖 Html.TextBoxFor(model.property)?
我想用我自己的助手重写 Html.TextBoxFor() ,该助手具有完全相同的签名(当然,但名称空间不同) - 这可能吗?如果可以,如何实现?
原因是我在现有的应用程序中有 100 多个视图,并且我想更改 TextBoxFor 的行为,以便在属性具有 [StringLength(n)] 注释时输出 maxLength=n 属性。
自动输出 maxlength=n 的代码在这个问题中: Asp.Net MVC 中 DataAnnotations StringLength 的文本框的 maxlength 属性。但我的问题不是重复的 - 我正在尝试创建一个更通用的解决方案:DataAnnotation 自动流入 html,而不需要编写视图的人编写额外的代码。
在引用的问题中,您必须将每个 Html.TexBoxFor 更改为 Html.CustomTextBoxFor。我需要这样做,以便现有的 TextBoxFor() 不需要更改 - 因此创建一个具有相同签名的帮助器:更改帮助器方法的行为,并且所有现有实例将无需任何更改即可工作(100 + 视图,至少 500 个 TextBoxFor() - 不想手动编辑)。
我尝试了这段代码:(并且我需要为 TextBoxFor 的每次重载重复它,但是一旦解决了根本问题,这将是微不足道的)
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() 的视图中收到编译器错误:“The以下方法或属性之间的调用是不明确的”(当然)。有什么办法可以做到这一点吗?
是否有另一种方法可以允许我更改 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?
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.
The code for automatically outputting maxlength=n is in this question: maxlength attribute of a text box from the DataAnnotations StringLength in Asp.Net MVC. 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.
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).
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
}
}
}
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?
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您不能同时拥有两个具有相同名称和相同签名的扩展方法。您可以将扩展方法放入自定义命名空间中,并使用此命名空间而不是 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:
but if you do this you will lose all the other extension methods and you will need to override them in your custom namespace.
简而言之,不,您不能“替换”现有的扩展方法。
更长的答案,你也许可以通过一些极其邪恶的反思来做到这一点......尽管我非常怀疑这是否有效。沿着这些思路:
Short answer, no, you can't "replace" an existing extension method.
Longer answer, you might be able to do so by some extremely evil reflection...Although I highly doubt even this would work. Something along these lines:
您可以使用编辑器模板和自定义 ModelMetadataProvider 来解决此问题。 (很抱歉没有提供更多信息,尽管这在谷歌上很容易找到,我希望这会给你带来正确的方向。)
You can solve this with a editortemplate and a custom ModelMetadataProvider. (Sorry for not giving more info, though this is quite Googleable and I hope this will put you in the right direction.)