如何在MVC Url.Action中调用不同控制器中的不同方法?

时间:2014-06-11 13:44:31

标签: asp.net-mvc controller url.action

在我的视图中,我使用的是此表单方法:

<form method="get" action="@Url.Action("Index")">

所以“Index”是Controller中的方法 - UserA:

public ActionResult Index(SearchParameters parameters, UserModel userModel)

我的问题是,如何添加这个相同的View新表单方法,我想在不同的Controller中调用action,所以我会调用... action =“@ Url.Action(”Index2“)。 ..其中Index2是ActionResult Index2 ...在Controller - UserB。

感谢您的解释......

1 个答案:

答案 0 :(得分:3)

Url.Action有一个overload,它将控制器名称作为第二个参数,因此您应该能够复制现有代码并添加第二个参数:

<form method="get" action="@Url.Action("Index", "UserB")">

顺便说一句,您可以使用HtmlHelper BeginForm扩展方法进一步简化代码,该方法具有same overload

@using (Html.BeginForm("Index")) {
    // form fields here
}

@using (Html.BeginForm("Index", "UserB")) {
    // form fields here
}

这将为您呈现<form>结构。