c# - 传递到字典中的模型项的类型为 '`,但该字典需要类型为 'System.Collections.Generic.IEnumerable' 的模型项

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

这里我正在 MVC 中使用 WCf 服务并从该服务检索值并尝试在 View 中显示它。出现错误:

The model item passed into the dictionary is of type 'System.Collections.Generic.List` but this dictionary requires a model item of type '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;
        }

Controller 代码:

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>

有什么建议吗?

最佳答案

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

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

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

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

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

你应该有

@model IEnumerable<AddressDetails>

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

关于c# - 传递到字典中的模型项的类型为 '`,但该字典需要类型为 'System.Collections.Generic.IEnumerable' 的模型项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12577891/

相关文章:

c# - 为什么按回车键不会触发我的 Gtk.Entry 中的 KeyPressEvent?

javascript - ASP.NET 获取页面加载时的窗口大小

asp.net-mvc - Html.TextBoxFor() 不生成任何 html 标记

jQuery Ajax 帖子将 null 发送到我的 MVC Controller

jQuery 选择的插件样式不正确。很小

c# - 使用 MEF 加载多个插件实例

c# - PowerShell 是否编译脚本?

asp.net - 右对齐 statictextfield 控件的问题

asp.net-mvc - 如果用户将表单提交到仅 POST 操作并且其身份验证已超时,登录后重定向会导致 404 错误

c# - 从列表获取数据到 JavaScript MVC 3?