简体   繁体   中英

The model item passed into the dictionary is of type '` but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable'

Here i am consuming a WCf service in MVC and retrieving values from that service and trying to show it in a view.getting the error:

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'

Service Code:

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 Code:

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

View Code:

@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>

Any suggestion?

The clue is in the bit you missed out for the question - the expected element type of the sequence in the view, and the element type of the list.

Here's what you're creating in the model:

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

But here's what the model declares it needs:

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

You should have

@model IEnumerable<AddressDetails>

My guess is that you copied and pasted the model declaration without looking - always make sure you understand every line of what you copy and paste.

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

return View(objAddressDetails.AsEnumerable());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM