2012-06-11 48 views
0

返回一個觀點我有這個帖子的方法:從Post方法

[HttpPost] 
    [ValidateAntiForgeryToken] 
    public ActionResult Invitations(SuperInvitationsEditModel model) 
    { 
     ... 

     var newmodel = new SuperInvitationsEditModel(); 

     if (hasErrors) 
     { 
      SuperInvitationsErrorModel newErrorModel = new SuperInvitationsErrorModel(); 
      newErrorModel.Errors = model.Errors; 
      return View(newErrorModel); 
     } 

     return View(newmodel); 
     } 

當如果(hasErrors)代碼執行我得到這個錯誤。

The model item passed into the dictionary is of type 'MyProject.Models.SuperInvitationsErrorModel', but this dictionary requires a model item of type 'MyProject.Models.SuperInvitationsEditModel'. 

我想我可以這樣做,因爲方法的返回值是一個通用的ActionResult。誰能告訴我爲什麼這不起作用?

回答

3

因爲您當前的視圖是強類型的。將代碼更改爲

return View("yourviewname",newErrorModel); 
2

它與鑄造ViewResultActionResult無關。問題是,您已強烈鍵入預期SuperInvitationsEditModel類型的模型(請參閱Invitations.cshtml頂部的@model),但您正在將SuperInvitationsErrorModel類型的模型傳遞給它。

你應該兩個視圖模型類(SuperInvitationsEditModelSuperInvitationsErrorModel)合併成一個,或者創建他們每個人的獨立觀點。