【问题标题】:How to call different method in different Controllers in MVC Url.Action?如何在 MVC Url.Action 的不同控制器中调用不同的方法?
【发布时间】:2014-08-01 14:33:12
【问题描述】:

在我看来,我正在使用这种表单方法:

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

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

public ActionResult Index(SearchParameters parameters, UserModel userModel)

我的问题是,如何在同一个 View 新表单方法中添加我想在不同控制器中调用操作的地方,所以我会例如调用 ... action="@Url.Action("Index2") 。 .. 其中 Index2 是 ActionResult Index2 ... 在 Controller - UserB 中。

感谢解释...

【问题讨论】:

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


    【解决方案1】:

    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
    }
    

    这将为您呈现 &lt;form&gt; 结构。

    【讨论】:

    • 不客气!我还添加了一个关于如何呈现表单的建议——也有一些内置的帮助器。
    猜你喜欢
    • 2010-11-20
    • 1970-01-01
    • 2021-07-26
    • 2017-09-24
    • 2023-03-11
    • 2013-06-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多