I am receiving this error when trying to use RedirectToAction, can anyone offer any advice on why this could be occuring, ive used this before without any problems, i must be missing something.
当我尝试使用RedirectToAction时,我收到了这个错误,有人能给我建议为什么会发生这种情况吗?
Cannot implicitly convert type 'System.Web.Mvc.RedirectToRouteResult' to 'System.Web.Mvc.ViewResult'
不能隐式转换type 'System.Web.Mvc。RedirectToRouteResult”“System.Web.Mvc.ViewResult”
[HttpPost]
public ViewResult Edit(Customer customer)
{
if (ModelState.IsValid)
{
customersRepository.SaveCustomer(customer);
TempData["message"] = customer.CustomerName + " has been saved.";
return RedirectToAction("Index");
}
else //validation error, so redisplay the same view
return View(customer);
}
Regards
问候
Liam
利亚姆
1 个解决方案
#1
20
Try changing public ViewResult Edit(Customer customer)
to public ActionResult Edit(Customer customer)
尝试将public ViewResult编辑(Customer)更改为public ActionResult编辑(Customer)
ViewResult is derived from ActionResult and can only return Views. Since your code can return a View or a Redirect, you should use an ActionResult. See this answer for more information.
ViewResult派生自ActionResult,只能返回视图。因为代码可以返回视图或重定向,所以应该使用ActionResult。有关更多信息,请参见此答案。
#1
20
Try changing public ViewResult Edit(Customer customer)
to public ActionResult Edit(Customer customer)
尝试将public ViewResult编辑(Customer)更改为public ActionResult编辑(Customer)
ViewResult is derived from ActionResult and can only return Views. Since your code can return a View or a Redirect, you should use an ActionResult. See this answer for more information.
ViewResult派生自ActionResult,只能返回视图。因为代码可以返回视图或重定向,所以应该使用ActionResult。有关更多信息,请参见此答案。