2012-03-15 82 views
1

我收到以下錯誤:MVC 3傳遞到字典的模型項目是A型,但本字典,需要B型的模型項目

The model item passed into the dictionary is of type '...Models.VideoPostingModel', but this dictionary requires a model item of type '...Models.RegisterModel'. 

我不知道是什麼問題,因爲型號匹配...

控制器:

使用系統; using System.Collections.Generic;使用System.Linq的 ; using System.Web; using System.Web.Mvc; 使用stayyolo.Models; using System.Web.Security; using System.Data;

...Controllers 
{ 
    public class VidsPostingController : Controller 
    { 

     private dbEntities db = new dbEntities(); 
     // 
     // GET: /VidsPosting/ 



     public ActionResult Details(Guid id) 
     { 
      Posting posting = db.Postings.Find(id); 
      if (posting.Image == null) 
      { 
       posting.Image = new byte[0]; 
       db.Entry(posting).State = EntityState.Modified; 
       db.SaveChanges(); 
      } 
      //convert ENTITY MODEL CLASS TO Model 
      VideoPostingModel toRet = new VideoPostingModel(); 
      toRet.linkIfVideo = posting.LinkIfVideo; 
      toRet.PostDate = posting.PostDate; 
      toRet.Titile = posting.Titile; 
      toRet.TypeOfPosting = posting.TypeOfPosting; 
      return View(toRet); 
     } 

} 

型號:

public class VideoPostingModel 
{ 
    ..Variables 

    public VideoPostingModel() 
    { 
     ... 

    } 
} 


View: 

@model ...Models.VideoPostingModel 

@{ 
    ViewBag.Title = "Details"; 
} 

<fieldset> 
    <legend>Posting</legend> 
    <div class="display-label"> 
     Description</div> 
    <div class="display-field"> 
     @Html.DisplayFor(model => model.Description) 
    </div> 
    <div class="display-label"> 
     Title</div> 
    <div class="display-field"> 
     @Html.DisplayFor(model => model.Titile) 
    </div> 
    <div class="display-label"> 
     Post Date</div> 
    <div class="display-field"> 
     @Html.DisplayFor(model => model.PostDate) 
    </div> 
    <div class="display-label"> 
     Type Of Posting</div> 
    <div class="display-field"> 
     @Html.DisplayFor(model => model.TypeOfPosting) 
    </div> 
</fieldset> 


     <div id="youtubePlayerdiv""> 
     <p style="text-align: justify"> 
     Video!</p> 
     <iframe id="youtubePlayer" class="youtube-player" type="text/html" 
     width="640" height="385" src="http://www.youtube.com/embed/<%=Model.LinkIfVideo%>" frameborder="0"> 
     </iframe> 
     </div> 
     <!-- end youtubeplayerDiv div --> 

我非常感謝提前的幫助下,當網站的點擊控制器的詳細方法出現正確的問題。

+0

是否全部代碼?你能顯示RegisterModel嗎? – 2012-03-15 06:20:12

+0

ResgisterModel只是創建該項目時獲得的基本MVC標準模型。 – Aziz 2012-03-15 14:54:34

+0

什麼? @Sasha – Aziz 2012-03-16 05:18:21

回答

1

你所得到的錯誤意味着某處您的視圖或_layout您曾經試圖使另一部分是這樣的:

@Html.Partial("~/Views/Account/Register.cshtml") 

但是這部分需要不同的模型 - RegisterModel。因此,有一種可能性是在渲染時將此模型的新實例傳遞給局部:

@Html.Partial("~/Views/Account/Register.cshtml", new RegisterModel()) 
+0

這不是問題,但我只是使用了一個尷尬的解決方案,並通過視圖包傳遞了所有數據。謝謝! – Aziz 2012-03-15 15:27:10

+0

但讓我知道,如果你想出了它的原因......這是奇怪的 - 我現在不能使用模型... – Aziz 2012-03-16 05:17:53

+0

有奇怪的AJAX這是正確的! – Aziz 2012-04-01 20:10:42

相關問題