user3509742
user3509742

Reputation: 47

foreach statement cannot operate because does not contain a public definition for 'GetEnumerator'

I build a solution has 4 layers


the problem is I want call a list search methods in DAL(Establishment) through BLL with Para Name to search it , and want to show the result in View in PL. the EstController

public ActionResult Index(string Name)
    {
        if (Name != null)
        {
            IList list = BLL.Establishment_Serv.getEstablishmentByName(Name.ToUpper());
            return View(list);
        }
        return View();
    }

and Est/Index View

    @using (Html.BeginForm("Index" ,"Est",FormMethod.Get))
{
   <p>
        Find by name: @Html.TextBox("Name")  
        <input type="submit" value="Search" />
    </p>
}
<table>
@foreach (var item in Model) 
{
    <tr>
        <td>@Html.DisplayFor(modelItem => item.Id)</td>
        <td>@Html.DisplayFor(modleItem => item.Name)</td>
    </tr>
}
</table>

and I face an error in foreach statement.And if there a best approach to doing it HINT me please.

Upvotes: 2

Views: 10884

Answers (2)

Mir Gulam Sarwar
Mir Gulam Sarwar

Reputation: 2648

if your list is null then you will get error

public ActionResult Index(string Name)
    {
        var list=new List<YourModelName>();
        if (Name != null)
        {
            list = BLL.Establishment_Serv.getEstablishmentByName(Name.ToUpper());
            return View(list);
        }
        else {
        return View(list);
}
    }

Upvotes: 0

Ni3
Ni3

Reputation: 489

Have you put this inside your view:

@model IEnumerable<YourModel>

Upvotes: 5

Related Questions