2011-06-03 28 views
12

我在嘗試使用RedirectToAction時收到此錯誤,任何人都可以提供任何建議,可能會發生,我之前使用這個沒有任何問題,我必須錯過一些東西。RedirectToAction MVC2的問題 - 不能將類型'System.Web.Mvc.RedirectToRouteResult'隱式轉換爲'System.Web.Mvc.ViewResult'

不能鍵入 '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); 

    } 

問候

利亞姆

回答

19

嘗試改變public ViewResult Edit(Customer customer)public ActionResult Edit(Customer customer)

ViewResult派生自ActionResult並且只能返回Views。既然你的代碼可以返回一個View或者一個重定向,你應該使用一個ActionResult。有關更多信息,請參閱this answer

+0

謝謝你,我認爲這可能是簡單的事情。 – liam 2011-06-03 12:13:10

+0

你知道這是爲什麼嗎? – 2012-05-04 07:35:37

+0

我更新了我的答案。基本上,'ViewResult'只能返回一個'View',而不是'RedirectToAction' – jao 2012-05-04 14:02:53

相關問題