RedirectToAction MVC2的问题 - 无法将类型'System.Web.Mvc.RedirectToRouteResult'隐式转换为'System.Web.Mvc.ViewResult'

lia*_*iam 14 asp.net redirecttoaction asp.net-mvc-2

我在尝试使用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);

    }
Run Code Online (Sandbox Code Playgroud)

问候

利亚姆

jao*_*jao 21

尝试public ViewResult Edit(Customer customer)改为public ActionResult Edit(Customer customer)

ViewResult派生自ActionResult,只能返回Views.由于您的代码可以返回视图或重定向,因此您应该使用ActionResult.有关更多信息,请参阅此答案.