SINFER
SINFER

Reputation: 177

The model item passed into the dictionary is of type 'System.Linq.Enumerable

I tried this code once without any exceptions. But now when i do it again in the same way i did it once i get this Exception saying,

The model item passed into the dictionary is of type 'System.Linq.Enumerable+WhereListIterator`1[myapp12.Models.Customer]', but this dictionary requires a model item of type 'myapp12.Models.Customer'.

This is my Code

CustomerController

**public class CustomerController : Controller
    {
        //
        // GET: /Customer/
        List<Customer> CustomerCollection = new List<Customer>();
        public CustomerController()
        {

            Customer cus = new Customer();
            cus.CustomerId = 1;
            cus.Name = "Mags";
            cus.Gender = "Male";
            cus.City = "jerks";
            CustomerCollection.Add(cus);
            cus = new Customer();
            cus.CustomerId = 2;
            cus.Name = "Jacob";
            cus.Gender = "Male";
            cus.City = "Wagga";
            CustomerCollection.Add(cus);
            cus = new Customer();
            cus.CustomerId = 3;
            cus.Name = "Gags";
            cus.Gender = "Male";
            cus.City = "NewYork";
            CustomerCollection.Add(cus);
        }
        public ActionResult GetCustomerList()
        {
            return View(CustomerCollection);
        }
        public ActionResult GetCustomer(int id)
        {
            var selectedCustomer = CustomerCollection.Where(p => p.CustomerId == id);
            return View(selectedCustomer);
        }**

GetCustomerList.aspx (view)

<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<IEnumerable<myapp12.Models.Customer>>" %>

<!DOCTYPE html>

<html>
<head runat="server">
    <meta name="viewport" content="width=device-width" />
    <title>GetCustomerList</title>
</head>
<body>
    <p>
        <%: Html.ActionLink("Create New", "Create") %>
    </p>
    <table>
        <tr>
            <th>
                <%: Html.DisplayNameFor(model => model.Name) %>
            </th>
            <th>
                <%: Html.DisplayNameFor(model => model.Gender) %>
            </th>
            <th>
                <%: Html.DisplayNameFor(model => model.City) %>
            </th>
            <th></th>
        </tr>

    <% foreach (var item in Model) { %>
        <tr>
            <td>
                <%: Html.DisplayFor(modelItem => item.Name) %>
            </td>
            <td>
                <%: Html.DisplayFor(modelItem => item.Gender) %>
            </td>
            <td>
                <%: Html.DisplayFor(modelItem => item.City) %>
            </td>
            <td>
                <%: Html.ActionLink("Edit", "Edit", new { id=item.CustomerId }) %> 
                <%: Html.ActionLink("Details", "GetCustomer", new { id = item.CustomerId })%> 
                <%: Html.ActionLink("Delete", "Delete", new { id=item.CustomerId }) %>
            </td>
        </tr>
    <% } %>

    </table>
</body>
</html>

GetCustomer.aspx(view) this is getting details of a specific user.

<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<myapp12.Models.Customer>" %>

<!DOCTYPE html>

<html>
<head runat="server">
    <meta name="viewport" content="width=device-width" />
    <title>GetCustomer</title>
</head>
<body>
    <fieldset>
        <legend>Customer</legend>

        <div class="display-label">
            <%: Html.DisplayNameFor(model => model.Name) %>
        </div>
        <div class="display-field">
            <%: Html.DisplayFor(model => model.Name) %>
        </div>

        <div class="display-label">
            <%: Html.DisplayNameFor(model => model.Gender) %>
        </div>
        <div class="display-field">
            <%: Html.DisplayFor(model => model.Gender) %>
        </div>

        <div class="display-label">
            <%: Html.DisplayNameFor(model => model.City) %>
        </div>
        <div class="display-field">
            <%: Html.DisplayFor(model => model.City) %>
        </div>
    </fieldset>
    <p>

        <%: Html.ActionLink("Edit", "Edit", new { id=Model.CustomerId }) %> |
        <%: Html.ActionLink("Back to List", "GetCustomerList") %>
    </p>
</body>
</html>

this worked for me first time when i was doing first time. but now not working? wht should i do?

Upvotes: 0

Views: 6455

Answers (1)

Wiktor Zychla
Wiktor Zychla

Reputation: 48230

You have

  var selectedCustomer = 
  CustomerCollection.Where(p => p.CustomerId == id);

where you should have

  var selectedCustomer = 
  CustomerCollection.Where(p => p.CustomerId == id).FirstOrDefault();

Without getting the first element of the list you are actually passing an enumerable list instead of a single item.

Upvotes: 3

Related Questions