Asp.net mvc 从另一个操作调用控制器中的操作方法
我有一个视图“Details”,我在其中使用Asp.net mvc 从另一个操作调用控制器中的操作方法,asp.net-mvc,asp.net-mvc-4,Asp.net Mvc,Asp.net Mvc 4,我有一个视图“Details”,我在其中使用@Html.Begin.Form(“Method1”,“Controller1”,FormMethod.Post)。单击提交时,将调用Method1操作 从Method1,我需要调用同一控制器中的另一个操作,称为Method2,并带有一个输入参数。Method1和Method2都返回ActionResult 每次调用Method2,或者 Redirect.ToAction("Method2", "Controller1", new {parameter
@Html.Begin.Form(“Method1”,“Controller1”,FormMethod.Post)
。单击提交时,将调用Method1
操作
从Method1
,我需要调用同一控制器中的另一个操作,称为Method2
,并带有一个输入参数。Method1
和Method2
都返回ActionResult
每次调用Method2
,或者
Redirect.ToAction("Method2", "Controller1", new {parameter});
或
该参数变为null,并弹出一个错误,说明“没有视图支持搜索的位置或找不到主视图”
另外,
Method2
是[HttpPost]
重定向。ToAction
发送Get
请求,因为您的操作是HttpPost
,所以它找不到操作,但您可以尝试此操作
return Method2(1,"parameter2");
假设您的方法2
是
[HttpPost]
public ActionResult Method2 (int someValue, string anotherValue) {
return View();
}
使用参数从Method1
调用操作Method2
:
[HttpPost]
public ActionResult Method2()
{
ViewData.Add(new KeyValuePair<string, object>("param2", "param2Value"));
ViewData.Add(new KeyValuePair<string, object>("param2", "param2Value"));
return View();
}
[HttpPost]
公共行动结果方法2()
{
添加(新的KeyValuePair(“param2”、“param2Value”);
添加(新的KeyValuePair(“param2”、“param2Value”);
返回视图();
}
并在视图中使用此参数:
@{
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....
@{
ViewBag.Title=“主页”;
Layout=“~/Views/Shared/_Layout.cshtml”;
字符串param1=Convert.ToString(ViewData[“param1”]);
字符串param2=Convert.ToString(ViewData[“param2]”);
}
这里是Html代码。。。。
问题已经解决。我使用下面的代码来解决它
Redirect.ToAction("Method2","Controller1", new {param = parameter});
不能重定向到POST方法
Redirect.ToAction("Method2","Controller1", new {param = parameter});