【问题标题】:The model item passed into the dictionary is of type 'System.Collections.Generic.List`1[System.String]' but requires type "Something else"传入字典的模型项的类型为“System.Collections.Generic.List`1[System.String]”,但需要“其他”类型
【发布时间】:2014-02-11 06:26:43
【问题描述】:

我正在使用以下代码..

控制器:

 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.List1[System.String]', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable1[farma.Models.XYZMaster]。

请帮助... TIA

【问题讨论】:

标签: c# asp.net-mvc


【解决方案1】:

您的ViewModel 的类型是IEnumerable&lt;Pharman.Models.XYZMaster&gt;,但您正在传递List&lt;string&gt;

您应该将ViewModel 类型更改为IEnumerable&lt;string&gt;

然后:

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

    @comment
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多