我有一个视图,"详细信息",我在其中使用了@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,并弹出一个错误,指出"没有视图支持搜索的位置或找不到主节点"。
此外,Method2
[HttpPost]
.
Redirect.ToAction
发送Get
请求,因为您的操作HttpPost
因此它找不到action
但您可以尝试此操作
return Method2(1,"parameter2");
假设您的Method2
是
[HttpPost]
public ActionResult Method2 (int someValue, string anotherValue) {
return View();
}
问题已解决。我使用以下代码来解决它
Redirect.ToAction("Method2","Controller1", new {param = parameter});
使用参数从Method1
调用操作Method2
:
[HttpPost]
public ActionResult Method2()
{
ViewData.Add(new KeyValuePair<string, object>("param2", "param2Value"));
ViewData.Add(new KeyValuePair<string, object>("param2", "param2Value"));
return 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....