【问题标题】:The model item passed into the dictionary is of type '` but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable'传入字典的模型项是“`”类型,但该字典需要一个“System.Collections.Generic.IEnumerable”类型的模型项
【发布时间】:2012-09-25 07:10:39
【问题描述】:

在这里,我在 MVC 中使用 WCf 服务并从该服务中检索值并尝试在视图中显示它。得到错误:

传入字典的模型项属于“System.Collections.Generic.List”类型,但该字典需要一个“System.Collections.Generic.IEnumerable”类型的模型项

服务代码:

public IList<AddressDetails> GetAddressDetails(string addressid)
        {
            List<AddressDetails> addressdetails = new List<AddressDetails>();
             {
                con.Open();
                SqlCommand cmd = new SqlCommand("select Name,EmailAddress,Line1,City from Address where addressid ='6742596A-F413-4C71-8BAB-0016F96B56A0'", con);
                SqlDataAdapter da = new SqlDataAdapter(cmd);
                DataTable dt = new DataTable();
                da.Fill(dt);
                if (dt.Rows.Count > 0)
                {
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        AddressDetails addressInfo = new AddressDetails();
                        //addressInfo.Addressid = dt.Rows[i]["Addressid"].ToString();
                        addressInfo.Name = dt.Rows[i]["Name"].ToString();
                        addressInfo.EmailAddress = dt.Rows[i]["EmailAddress"].ToString();
                        addressInfo.Line1 = dt.Rows[i]["Line1"].ToString();
                        addressInfo.City = dt.Rows[i]["City"].ToString();
                        addressdetails.Add(addressInfo);
                    }
                }
                con.Close();
            }
            return addressdetails;
        }

控制器代码:

ServiceReference1.Service1Client objService = new ServiceReference1.Service1Client();
        public ActionResult sample()
        {
            IList<AddressDetails> objAddressDetails = new List<AddressDetails>();
            objAddressDetails = objService.GetAddressDetails("");
            return View(objAddressDetails.ToList());
        }

查看代码:

@model IEnumerable<Magelia.WebStore.StarterSite.Web.Models.Sample.SampleViewModel>

@{
    ViewBag.Title = "sample";
}

<h2>sample</h2>

<p>
    @Html.ActionLink("Create New", "Create")
</p>
<table>
    <tr>
        <th>
            AddressId
        </th>
        <th>
            Name
        </th>
        <th>
            EmailAddress
        </th>
        <th>
            Line1
        </th>
        <th>
            City
        </th>
        <th></th>
    </tr>

@foreach (var item in Model) {
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.AddressId)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Name)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.EmailAddress)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Line1)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.City)
        </td>
        <td>
            @Html.ActionLink("Edit", "Edit", new { /* id=item.PrimaryKey */ }) |
            @Html.ActionLink("Details", "Details", new { /* id=item.PrimaryKey */ }) |
            @Html.ActionLink("Delete", "Delete", new { /* id=item.PrimaryKey */ })
        </td>
    </tr>
}

</table>

有什么建议吗?

【问题讨论】:

  • 您的控制器中有 IList,但视图中有一些 IEnumerable ......问题可能是@ 987654324@ 不能转换为 ...SampleViewModel

标签: c# asp.net-mvc asp.net-mvc-3 wcf razor


【解决方案1】:

线索就在您错过的问题中 - 视图中序列的预期元素类型,以及列表的元素类型

这是您在模型中创建的内容:

IList<AddressDetails> objAddressDetails = new List<AddressDetails>();

但这是模型声明它需要的:

@model IEnumerable<Magelia.WebStore.StarterSite.Web.Models.Sample.SampleViewModel>

你应该有

@model IEnumerable<AddressDetails>

我的猜测是您复制并粘贴了模型声明而没有查看 - 始终确保您了解复制和粘贴的每一行。

【讨论】:

    【解决方案2】:

    尝试更改以下代码行(在控制器中):

    return View(objAddressDetails.AsEnumerable());
    

    【讨论】:

    • 我不认为这是问题所在。请参阅我的答案,以了解我怀疑确实是错误的。 IList&lt;T&gt; 已经扩展 IEnumerable&lt;T&gt;
    猜你喜欢
    • 2022-01-16
    • 2015-09-19
    • 2013-10-17
    • 2015-09-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-21
    相关资源
    最近更新 更多