从另一个动作调用控制器中的动作方法

时间:2017-06-15 09:19:35

标签: asp.net-mvc asp.net-mvc-4

我有一个“详细信息”视图,其中我正在使用@Html.Begin.Form ("Method1","Controller1",FormMethod.Post)

现在点击提交操作“Method1”被调用。 从“Method1”我需要在同一个控制器中调用另一个带有输入参数的“Method2”。请注意,“Method1”和“Method2”都是ActionResult。

每次我使用

调用Method2

Redirect.ToAction(“Method2”,“Controller1”,new {parameter});
Redirect.ToAction((“Method2”,new {parameter});

参数变为null并弹出错误,说没有视图支持搜索到的位置或找不到master。

“Method2”也是[HttpPost]

3 个答案:

答案 0 :(得分:0)

Redirect.ToAction sends Get request since your action is HttpPost so it does not finds the action but you can try this

return Method2(1,"parameter2");

suppose your Method2 is

[HttpPost]
public ActionResult Method2 (int someValue, string anotherValue) {
   return View();
}

答案 1 :(得分:0)

    Call the Action Method2 from Method1 with parameters
     [HttpPost]
        public ActionResult Method2()
        {
            ViewData.Add(new KeyValuePair<string, object>("param2", "param2Value"));
            ViewData.Add(new KeyValuePair<string, object>("param2", "param2Value"));
            return View();
        }

and user this paramete in your view:

    @{
    ViewBag.Title = "Home";
    Layout = "~/Views/Shared/_Layout.cshtml";

    string param1  = Convert.ToString(ViewData["param1"]);
    string param2  = Convert.ToString(ViewData["param2"]);
}
<script>
</script>
Html code here....

答案 2 :(得分:0)

问题已解决。我用下面的代码来解决它

Redirect.ToAction("Method2","Controller1", new {param = parameter});