Click here to Skip to main content
15,880,503 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi,
I am passing a list values to my View. But I am getting an error of "The model item passed into the dictionary is of type 'System.Collections.Generic.List". Here is my controller action:
C#
public ActionResult Index()
       {
           Test t = new Test();
           List<Customer> myList= t.GetData();
           return View(myList);
       }

Here is the view:
HTML
@model List<AsyncActions.Models.Test>
    @{
      ViewBag.Title = "Index";
    }

    <h2>Index</h2>
    <table border="1" cellpadding="4">
        @foreach (var row in Model)
        {
        <tr>
            @*<td>@row."CustomerID"</td>
            <td>@row."CustomerCode"</td>*@
        </tr>
    }
</table>

I have already tried removing List<> from view. When I do that, I am getting an error as "foreach statement cannot operate on variables of type 'AsyncActions.Models.Test' because 'AsyncActions.Models.Test' does not contain a public definition for 'GetEnumerator". I have tried so many thinks. I checked this link , but that didn't solved my issue. I am unable to find out what I have missed. Any help is much appreciated.
Posted
Updated 27-Feb-20 18:33pm

Your view has this as the model type

Razor
@model List<AsyncActions.Models.Test>


but you are passing this as the model

C#
List<Customer>


The type of the model in your view has to match what is being passed so it needs to be

Razor
@model List<Customer>


You might have to use the full Customer type name, including namespace.
 
Share this answer
 
As mentioned in above answer, below line can work:
Razor
@model List<Customer>

More specifically, it must be something like like:
@model
"IEnumerable or List or exact collection type which you are passing from action"
<"Fully qualified name of class in collection or use class name with using statement at top">

If you need working example, have a look at line, taken from code example given at:
Multiple Models in a View in ASP.NET MVC 4 / MVC 5[^]
Razor
@model  IEnumerable <MultipleModelDemo.Models.Student>


Thanks.
 
Share this answer
 
v4
@model IEnumerable<MvcApplication1.Models.Customer>

@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Index</title>
</head>
<body>
    <div>
        
         <table border="1">
        <tr>
            <th>
                CustomerCode
            </th>
            <th>
         CustomerId  
            </th>
        </tr>
        
            @foreach (var row in Model)
        {
                <tr>
            <th>@row.CustomerCode<br/></th>
            <th>@row.CustomerId<br/></th>
        </tr>
    }
            
             </table>
    </div>
</body>
</html>
 
Share this answer
 
v2
Comments
Member 14649820 14-Nov-19 7:52am    
I did the list of question & Answer of my online exam project.How to display the mark in view?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900