简体   繁体   English

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

[英]Multiple generic parameters on a html helper extension method

What I'm trying to do is create an extension method for the HtmlHelper to create a specific output and associated details like TextBoxFor<>. 我要做的是为HtmlHelper创建一个扩展方法来创建一个特定的输出和相关的细节,如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. 我想要做的是根据TextBoxFor <>从模型类中指定属性,然后是关联的控制器操作和其他参数。

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. 在我看来,如果我按照TextBoxFor调用它而不指定Model类型,我可以指定lambda表达式来设置它的属性,但是当我去指定我无法执行的操作时。

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. 但是,当我指定控制器类型Html.Create<HomeController>( ... )我无法指定要为其创建控件的模型属性。

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: 编辑#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?! 与提供的html辅助扩展方法相同的行可能只是将动作名称和控制器名称指定为字符串参数是要走的路?! But surely what I'm trying to do is possible? 但当然我想做的事情是可能的吗? hits head 打头

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

<%= Html.Create(x => x.Title, (HomeController c) => c.Index(), 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) 你别无选择,只能调用它(假设你的TModel是Book类型,标题是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. 问候。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM