RedirectToAction MVC2 的问题 - 无法隐式转换类型“System.Web.Mvc.RedirectToRouteResult”到“System.Web.Mvc.ViewResult”
我在尝试使用 RedirectToAction 时收到此错误,任何人都可以提供有关为什么会发生这种情况的任何建议吗?我之前使用过这个,没有任何问题,我一定错过了一些东西。
无法将类型“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);
}
问候
Liam
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.
Cannot implicitly convert type 'System.Web.Mvc.RedirectToRouteResult' to '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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试将
public ViewResult Edit(Customer customer)
更改为public ActionResult Edit(Customer customer)
ViewResult 派生自 ActionResult,只能返回 View。由于您的代码可以返回视图或重定向,因此您应该使用 ActionResult。有关详细信息,请参阅此答案。
Try changing
public ViewResult Edit(Customer customer)
topublic ActionResult Edit(Customer 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.