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

SINFER

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?

Wiktor Zychla

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.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

model item passed into the dictionary is of type 'System.Collections.Generic.List, but this dictionary requires a model item of type

From Dev

model item passed into the dictionary is of type 'System.Boolean', but this dictionary requires a model item of type 'MyProject.checkboxstate'

From Dev

The model item passed into the dictionary is of type 'System.Net.Http.HttpResponseMessage',

From Dev

The model item passed into the dictionary is of type 'System.Data.Entity.DbSet

From Dev

MVC: The model item passed into the dictionary is of type System.Int32

From Dev

The model item passed into the dictionary is of type ..., but this dictionary requires ...

From Java

The model item passed into the dictionary is of type .. but this dictionary requires a model item of type

From Dev

The model item passed into the dictionary is of type ' ', but this dictionary requires a model item of type ' '

From Dev

The model item passed into the dictionary is of type 'System.Data.Entity.Infrastructure.DbQuery', but this dictionary requires a model item of type B

From Dev

The model item passed into the dictionary is of type 'System.Collections.Generic.List`1[X]', but this dictionary requires a model item of type 'X'

From Dev

The model item passed into the dictionary is of type 'System.Data.Entity.Infrastructure.DbQuery', but this dictionary requires a model item of type B

From Dev

The model item passed into the dictionary is of type 'System.Int32', but this dictionary requires a model item of type 'IMS.Models.Dealer'

From Dev

The model item passed into the dictionary is of type error

From Dev

The model item passed into the dictionary is of type 'System.Threading.Tasks.Task`1[System.Collections.Generic.IEnumerable`

From Dev

Exception Details: System.InvalidOperationException: The model item passed into the dictionary is of type 'System.Collections.Generic.List`

From Dev

The model passed into dictionary is type 'TestApp.Models.Registration.RegistrationVm' but this dictionary requires a model item of type System.String

From Dev

The model item passed into the dictionary is of type 'System.Collections.Generic.List... in ASP.net MVC

From Dev

ASP.Net MVC "The model item passed into the dictionary is of type 'System.Collections.Generic.List"

From Dev

The model item passed into the dictionary is of type 'System.Data.Entity.Infrastructure.DbQuery

From Dev

The model item passed into the dictionary is of type 'System.Collections.Generic.List in Razor mvc5

From Dev

"The model item passed into the dictionary is of type" while passing model to a partial view

From Dev

The model item passed into the dictionary is of type view model but requires generic IEnum

From Dev

"The model item passed into the dictionary is of type" while passing model to a partial view

From Dev

The model item passed into the dictionary is of type 'Umbraco.Web.Models.RenderModel', but this dictionary requires a model item of type 'TestModel'

From Dev

The model item passed into the dictionary is of type 'BlogHomePageModel', but this dictionary requires a model item of type 'BlogHomePageModel'

From Dev

The model item passed into the dictionary is of type 'Sitecore.Mvc.Presentation.RenderingModel', but this dictionary requires a model item of type 'X'

From Dev

The model item passed into the dictionary is of type 'a', but this dictionary requires a model item of type 'b'

From Dev

How to fix The model item passed into the dictionary is of type error?

From Dev

How to fix The model item passed into the dictionary is of type error?

Related Related

  1. 1

    model item passed into the dictionary is of type 'System.Collections.Generic.List, but this dictionary requires a model item of type

  2. 2

    model item passed into the dictionary is of type 'System.Boolean', but this dictionary requires a model item of type 'MyProject.checkboxstate'

  3. 3

    The model item passed into the dictionary is of type 'System.Net.Http.HttpResponseMessage',

  4. 4

    The model item passed into the dictionary is of type 'System.Data.Entity.DbSet

  5. 5

    MVC: The model item passed into the dictionary is of type System.Int32

  6. 6

    The model item passed into the dictionary is of type ..., but this dictionary requires ...

  7. 7

    The model item passed into the dictionary is of type .. but this dictionary requires a model item of type

  8. 8

    The model item passed into the dictionary is of type ' ', but this dictionary requires a model item of type ' '

  9. 9

    The model item passed into the dictionary is of type 'System.Data.Entity.Infrastructure.DbQuery', but this dictionary requires a model item of type B

  10. 10

    The model item passed into the dictionary is of type 'System.Collections.Generic.List`1[X]', but this dictionary requires a model item of type 'X'

  11. 11

    The model item passed into the dictionary is of type 'System.Data.Entity.Infrastructure.DbQuery', but this dictionary requires a model item of type B

  12. 12

    The model item passed into the dictionary is of type 'System.Int32', but this dictionary requires a model item of type 'IMS.Models.Dealer'

  13. 13

    The model item passed into the dictionary is of type error

  14. 14

    The model item passed into the dictionary is of type 'System.Threading.Tasks.Task`1[System.Collections.Generic.IEnumerable`

  15. 15

    Exception Details: System.InvalidOperationException: The model item passed into the dictionary is of type 'System.Collections.Generic.List`

  16. 16

    The model passed into dictionary is type 'TestApp.Models.Registration.RegistrationVm' but this dictionary requires a model item of type System.String

  17. 17

    The model item passed into the dictionary is of type 'System.Collections.Generic.List... in ASP.net MVC

  18. 18

    ASP.Net MVC "The model item passed into the dictionary is of type 'System.Collections.Generic.List"

  19. 19

    The model item passed into the dictionary is of type 'System.Data.Entity.Infrastructure.DbQuery

  20. 20

    The model item passed into the dictionary is of type 'System.Collections.Generic.List in Razor mvc5

  21. 21

    "The model item passed into the dictionary is of type" while passing model to a partial view

  22. 22

    The model item passed into the dictionary is of type view model but requires generic IEnum

  23. 23

    "The model item passed into the dictionary is of type" while passing model to a partial view

  24. 24

    The model item passed into the dictionary is of type 'Umbraco.Web.Models.RenderModel', but this dictionary requires a model item of type 'TestModel'

  25. 25

    The model item passed into the dictionary is of type 'BlogHomePageModel', but this dictionary requires a model item of type 'BlogHomePageModel'

  26. 26

    The model item passed into the dictionary is of type 'Sitecore.Mvc.Presentation.RenderingModel', but this dictionary requires a model item of type 'X'

  27. 27

    The model item passed into the dictionary is of type 'a', but this dictionary requires a model item of type 'b'

  28. 28

    How to fix The model item passed into the dictionary is of type error?

  29. 29

    How to fix The model item passed into the dictionary is of type error?

HotTag

Archive