2014-02-11 68 views
1

我使用如下代碼..傳遞到字典的模型項的類型爲「System.Collections.Generic.List`1 [System.String]」,但需要輸入「別的東西」

控制器:

public ActionResult Comments(int? Id) 
    { 
     var abccomment= db.xyzComments.Include(c=>c.XYZMasters); 
    var comments = (from x in abccomment 
         where Id == x.CcId 
         select x.Comment).Distinct().ToList(); 

     return View(comments); 
    } 

和我的看法是

@model IEnumerable<Pharman.Models.XYZMaster> 
@foreach (var item in Model) 
    { 

     @item.XYZComment.Comment 
    } 

我得到以下錯誤: 傳遞到字典的模型項的類型爲「System.Collections.Generic.List 1[System.String]', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable 1 [farma.Models.XYZMaster]。

請幫助... TIA

+0

這是類似於這個職位: - http://stackoverflow.com/questions/4813307/the-model-item-passed-into-the-dictionary-is-of-type-but-this-dictionary-req – sankoobaba

回答

7

ViewModel的類型是IEnumerable<Pharman.Models.XYZMaster>但你逝去List<string>

你應該改變你的ViewModel類型IEnumerable<string>

然後:

@model IEnumerable<string> 
@foreach (var comment in Model) 
{ 

    @comment 
} 
+0

謝謝塞爾曼22 – ARC

相關問題