html 帮助器扩展方法上的多个通用参数

发布于 2024-08-29 01:12:37 字数 1293 浏览 9 评论 0原文

我想要做的是为 HtmlHelper 创建一个扩展方法来创建特定的输出和关联的详细信息,例如 TextBoxFor<>。我想要做的是根据 TextBoxFor<> 指定模型类中的属性,然后指定关联的控制器操作和其他参数。

到目前为止,该方法的签名如下所示:

public static MvcHtmlString Create<TModel, TProperty, TController>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression, Expression<Action<TController>> action, object htmlAttributes)
        where TController : Controller
        where TModel : class

当我去调用它时,就会出现问题。在我看来,如果我按照 TextBoxFor 调用它而不指定模型类型,我可以指定 lambda 表达式来设置它的属性,但是当我去指定操作时我无法这样做。

但是,当我指定控制器类型 Html.Create( ... ) 时,我无法指定要为其创建控件的模型属性。

我希望能够这样称呼它,就像

<%= Html.Create<HomeController>(x => x.Title, controller => controller.action, null) %>

过去一天我在这个问题上花了几个小时一样,有人能指出我正确的方向吗?

编辑: 感谢您对此的回复。

因此,无需指定我认为可以接受的所有类型,

<%= Html.Create(x => x.Title, ((HomeController)controller) => controller.action, null) %>

但仍然需要对操作的引用,而不是实际操作本身

*回到思考:)

编辑#2:

我开始思考试图使其成为纯粹的强类型有点牵强。遵循与提供的 html 帮助器扩展方法相同的思路,也许只需将操作名称和控制器名称指定为字符串参数就是正确的方法?!但我想做的事情肯定是可能的吗? 击中头部

What I'm trying to do is create an extension method for the HtmlHelper to create a specific output and associated details like TextBoxFor<>. What I want to do is specify the property from the model class as per TextBoxFor<>, then an associated controller action and other parameters.

So far the signature of the method looks like:

public static MvcHtmlString Create<TModel, TProperty, TController>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression, Expression<Action<TController>> action, object htmlAttributes)
        where TController : Controller
        where TModel : class

The issue occurs when I go to call it. In my view if I call it as per the TextBoxFor without specifying the Model type I am able to specify the lambda expression to set the property which it's for, but when I go to specify the action I am unable to.

However, when I specify the controller type Html.Create<HomeController>( ... ) I am unable to specify the model property that the control is to be created for.

I want to be able to call it like

<%= Html.Create<HomeController>(x => x.Title, controller => controller.action, null) %>

I've been hitting my head for a few hours now on this issue over the past day, can anyone point me in the right direction?

Edit:
Thanks for the responses to this.

So without specifying all of the types I think I can live with

<%= Html.Create(x => x.Title, ((HomeController)controller) => controller.action, null) %>

But still need the reference to the action, not the actual action itself

*back to thinking :)

Edit #2:

I'm starting to think trying to make it purely strongly typed is a bit far fetched. Going along the same lines as the provided html helper extension methods maybe just specifying the action name and controller name as string parameters is the way to go?! But surely what I'm trying to do is possible? hits head

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

霞映澄塘 2024-09-05 01:12:37

我认为你能得到的最接近的是:

<%= Html.Create(x => x.Title, (HomeController c) => c.Index(), null) %>

I think the closest you can get is:

<%= Html.Create(x => x.Title, (HomeController c) => c.Index(), null) %>
坚持沉默 2024-09-05 01:12:37

无法推断类型参数。您别无选择,只能像这样调用它(假设您的 TModel 是 Book 类型,而 Title 是 string 类型)

<%= Html.Create<Book, string, HomeController>(x=>x.Title, controller=>controller.SomeAction(), null) %>

,否则它将不知道应该在哪个控制器上调用该操作。

问候。

The type parameters can not be inferred. You have no choice but to invoke it like (supposing your TModel is of type Book and the Title is of type string)

<%= Html.Create<Book, string, HomeController>(x=>x.Title, controller=>controller.SomeAction(), null) %>

Otherwise it will not know over which controller it should invoke the action.

Regards.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文