从其他帖子返回视图

时间:2018-09-26 08:26:24

标签: c# asp.net asp.net-mvc-5

这是我的代码:

[HttpPost]
public ActionResult Edit(int id)
{
   var viewModel = service.GetUserViewModel(id);
   return View(viewModel); // reponse the ValidateLogin view
}

[HttpPost]
public ActionResult EditDetails(UserDetails userDetails)
{
    return Edit(userDetails.Id);
}

不幸的是,它没有返回视图,但是却给了我错误。怎么了?

1 个答案:

答案 0 :(得分:0)

您无法使用[HttpPost]从标有return Edit(userDetails.Id)的其他动作中返回视图,而是必须将一个动作设置为[HttpGet](即Edit动作方法)并使用RedirectToAction() overload which has route value parameter调用该操作,如下例所示:

[HttpPost]
public ActionResult EditDetails(UserDetails userDetails)
{
    // note: the second overload is routeValues
    return RedirectToAction("Edit", userDetails.Id);
}

[HttpGet]
public ActionResult Edit(int id)
{
   var viewModel = service.GetUserViewModel(id);
   return View(viewModel); // reponse the ValidateLogin view
}