【问题标题】:Problems with RedirectToAction MVC2 - Cannot implicitly convert type 'System.Web.Mvc.RedirectToRouteResult' to 'System.Web.Mvc.ViewResult'RedirectToAction MVC2 的问题 - 无法将类型“System.Web.Mvc.RedirectToRouteResult”隐式转换为“System.Web.Mvc.ViewResult”
【发布时间】:2011-09-07 18:45:44
【问题描述】:

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

    }

问候

利亚姆

【问题讨论】:

    标签: asp.net asp.net-mvc-2 redirecttoaction


    【解决方案1】:

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

    ViewResult 派生自 ActionResult,只能返回 Views。由于您的代码可以返回视图或重定向,因此您应该使用 ActionResult。请参阅this answer 了解更多信息。

    【讨论】:

    • 谢谢你,我认为这可能很简单。
    • 我更新了我的答案。基本上,ViewResult 只能返回 View,而不是 RedirectToAction
    猜你喜欢
    • 2014-07-23
    • 2021-03-15
    • 2011-05-28
    • 1970-01-01
    • 1970-01-01
    • 2020-01-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多