1

我有像这样的 html-layout 的 ascx 部分视图

<%=Html.ActionLink<PersonController>(x => x.Publications(param1, param2, ... )) %>

我的 ascx 很大,我想重用它,用另一个控制器/方法更改 Html.ActionLink 中的控制器/方法。另一个控制器的方法与 PersonController.Publications 具有相同的签名。请向我建议如何使控制器/方法可配置为我的布局的最佳方法。

先感谢您

4

1 回答 1

1

最简单的方法是将控制器名称和操作名称作为模型上的字符串。然后你可以使用actionlink的非强类型重载。像这样的东西:

<%=Html.ActionLink(Model.Action, Model.Controller, new { param1 = 1, param2 = 2 })%>

并像这样使用它:

<%Html.RenderPartial("PartialName", new PartialModel{Controller = "Person", Action = "Publications"})%>

如果你想使用强类型版本,你可以这样做:

//Model for your partial view
public class PartialModel<TController> where TController : Controller
{
    public Func<int, int, Expression<Action<TController>>> GetLinkAction { get; set; }
}

//Render the action link in your partial
<%=Html.ActionLink(Model.GetLinkAction(1, 2))%>

//Render the partialview in any page
<%Html.RenderPartial("PartialName", new PartialModel<PersonController> { GetLinkAction = (param1, param2) => x => x.Publications(param1, param2) })%>

You will of course have to adjust this for the parameters that you have. The nice thing about the strongly typed way is that the methods doesn't have to have the exact same signature and parameter names.

于 2010-05-07T09:52:57.947 回答