Problems with RedirectToAction MVC2 - Cannot implicitly convert type 'System.Web.Mvc.RedirectToRouteResult' to 'System.Web.Mvc.ViewResult'

只谈情不闲聊 提交于 2019-12-10 12:31:55

问题


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


回答1:


Try changing public ViewResult Edit(Customer customer) to public 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.



来源:https://stackoverflow.com/questions/6226811/problems-with-redirecttoaction-mvc2-cannot-implicitly-convert-type-system-web

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!