0

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:

public ActionResult Index()
    {
        Test t = new Test();
        List<Customer> myList= t.GetData();
        return View(myList);
    }

Here is the view:

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

here is my model:

namespace AsyncActions.Models
{
public class Test
{
    public List<Customer> GetData()
    {
        try
        {
            Customer c = new Customer();
            List<Customer> cst = new List<Customer>();
            for (int i = 0; i < 10; i++)
            {                    
                c.CustomerID = i;
                c.CustomerCode = "CST"+i;
                cst.Add(c);
            }
            return cst;
        }
        catch (Exception)
        {
            throw new NotImplementedException();
        }

    }
}
public class Customer
{
    public int CustomerID { get; set; }
    public string CustomerCode { get; set; }
}
}

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 things. 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.

Community
  • 1
  • 1
Sibeesh Venu
  • 18,755
  • 12
  • 103
  • 140
  • 2
    Looks like you have not published your actual code. ` List myList= t.GetData();` says you model is a `List`. But your view has something else as the model. – Kosala W Nov 23 '15 at 11:45
  • @kosala Please see the updated question. – Sibeesh Venu Nov 23 '15 at 11:50
  • I think you already have the answer below. This comment is not related to your question. There is a bug in your model. You should define `Customer c = new Customer();` inside the for loop. – Kosala W Nov 23 '15 at 12:10
  • @kosala Yes, I already got the answer. And many thanks for finding out the bug. – Sibeesh Venu Nov 23 '15 at 12:12

4 Answers4

2

Your View model should be

@model List<Customer>
Ibrahim Khan
  • 20,616
  • 7
  • 42
  • 55
  • You are right. I tried as you said, that also worked. Thank you , Finally I changed my model defenition to @model IEnumerable. That solved my problem. – Sibeesh Venu Nov 23 '15 at 11:58
2

Use this in your view should resolve the issue:

@model List<Customer>
tdat00
  • 810
  • 1
  • 8
  • 11
  • You are right. I tried as you said, that also worked. Thank you , Finally I changed my model defenition to @model IEnumerable. That solved my problem. – Sibeesh Venu Nov 23 '15 at 11:59
1

Change your @model to @model IEnumerable<Customer>

@model IEnumerable<Customer>
@{
  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>
}
Suren Srapyan
  • 66,568
  • 14
  • 114
  • 112
1

You should change the definition of your model to have:

@model List<Customer>
@{
  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>
}
LeBaptiste
  • 1,156
  • 14
  • 20
  • You are right. I tried as you said, that also worked. Thank you , Finally I changed my model defenition to @model IEnumerable. That solved my problem. – Sibeesh Venu Nov 23 '15 at 12:00