Click here to Skip to main content
15,886,639 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
foreach statement cannot operate on variables of type 'demo.mvc.Models.Book' because 'demo.mvc.Models.Book' does not contain a public definition for 'GetEnumerator'

Iam Getting This Error
Please solve this
Here is my code:
ASP
csHtml:
@{
    ViewBag.Title = "Index";
}
@model demo.mvc.Models.Book
<h2>
    Index</h2>
@*<form action ="@Url.Action()" method="post">
</form>*@
@Html.ValidationSummary(true)
@Html.BeginForm()
<fieldset>
    <fieldset>
        <table>
            <tr>
                @*<th>
                    BookId
                </th>*@
                <th>
                    @Html.DisplayNameFor(model => model.Name)
                </th>
                <td>
                    @Html.DisplayNameFor(model => model.Author)
                </td>
            </tr>
            @foreach (var item in Model)
            {
                <tr>
                    @*<td>
                    @Html.DisplayFor(model => model.BookId)
                </td>*@
                    <td>
                        @Html.DisplayFor(modelItem => modelItem.Name)
                    </td>
                    <td>
                        @Html.DisplayFor(modelItem => modelItem.Author)
                    </td>
                </tr>
            }
        </table>
    </fieldset>
   
</fieldset>



Controller:
C#
public ActionResult Index()
       {
           List<Book> bk = new List<Book>();
           SqlConnection con = new SqlConnection(@"data source=192.168.1.2\SqlSrv08Dev;DataBase=sa;user id=sa;pwd=1234;Application Name=MVC");
           con.Open();
           string query = "select * from Book ";
           SqlCommand cmd = new SqlCommand(query, con);
           SqlDataAdapter da = new SqlDataAdapter(cmd);
           cmd.CommandType = CommandType.Text;
           DataSet ds = new DataSet();
           da.Fill(ds);
           foreach (DataRow dr in ds.Tables[0].Rows)
           {
               bk.Add(new Book()
               {
                   Name = dr[0].ToString(),
                   Author = dr[1].ToString()
               });
           }
           cmd.ExecuteNonQuery();
           return View();


       }
[Edit]Code blocks added[/Edit]
Posted
Updated 4-Apr-13 0:07am
v4

1 solution

There are at least two things wrong here:

First the view is expecting a model of type demo.mvc.Models.Book The name implies that this is an individual book, so it would make sense for it to implement GetEnumerator which is intended for use with collections such as lists etc. The view is probably expecting the wrong type (though it could be the case that Book is a collection, in which case it should inherit from IEnumerable at the very least). You need to check what the type you should be using, I'd expected a View model containing a List<Book> or just the list passed directly (though this is less good practise IMO) given what is said in the rest of your code:

C#
csHtml:
@{
    ViewBag.Title = "Index";
}
@model List<demo.mvc.Models.Book>

Second your controller gets a list of books, but never passes the data in to the view, assuming you are binging to the list:

public ActionResult Index()
       {
           List<Book> bk = new List<Book>();
           //........... snip
           return View(bk);


       }


Given that this looks like a sample, I'd suggest you research Model-View-ViewModel (MVVM) in MVC, it'll save a lot of headache as you start to work on more complex things.
 
Share this answer
 
Comments
arthamsai 4-Apr-13 7:59am    
Thanx Keith
Keith Barrow 4-Apr-13 8:08am    
No problem

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