在MVC 4中显示列表 [英] To Show List in MVC 4

查看:44
本文介绍了在MVC 4中显示列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

foreach语句不能对''demo.mvc.Models.Book''类型的变量进行操作,因为''demo.mvc.Models.Book''不包含''GetEnumerator''的公共定义



Iam得到此错误

请解决此问题

这是我的代码:

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:

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:

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]

推荐答案

这里至少有两件事是错的:



首先该视图需要一个类型的模型。 mvc.Models.Book 这个名字暗示这是一本单独的书,所以它有意义实现 GetEnumerator ,它是打算使用的使用列表等集合。视图可能期望错误的类型(尽管可能是 Book 是一个集合,其中它应该至少从 IEnumerable 继承。您需要检查您应该使用的类型,我希望View模型包含 List< Book> 或者只是直接传递的列表(尽管这个更少)好的做法IMO)给出你的其余代码中的内容:



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:

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);


       }





鉴于这看起来像样品,我'建议你在MVC中研究Model-View-ViewModel(MVVM),当你开始处理更复杂的事情时,它会节省很多麻烦。



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.


这篇关于在MVC 4中显示列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆