在html页面中显示数据库表中的所有内容?

我需要将数据库中的所有记录显示到 HTML 页面。


@model QBKartMVC.Models.Products


@foreach (var item in Model)

            {

                 <tr>    

                    <td><input type="checkbox"></td>

                    <td>@Html.DisplayFor(x => x.ProductCode)</td>

                    <td>@Html.DisplayFor(x => x.ProductName)</td>

                    <td>@Html.DisplayFor(x => x.ProductDes)</td>

                    <td>@Html.DisplayFor(x => x.ActiveFlag)</td>

                    <td>@Html.DisplayFor(x => x.Price)</td>

                 </tr>

             }

错误显示表达式树可能不包含动态操作


public IActionResult Index()

        {

            var context = new DBContext();

            return View(context.Products.ToList());

        }

这是控制器部分


慕标5832272
浏览 151回答 3
3回答

慕运维8079593

更新您的视图代码如下,@model List<QBKartMVC.Models.Products>@for (var i = 0; i < Model.Count; i++){&nbsp; &nbsp; &nbsp;<tr>&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;<td><input type="checkbox"></td>&nbsp; &nbsp; &nbsp; &nbsp;<td>@Html.DisplayFor(x => x[i].ProductCode)</td>&nbsp; &nbsp; &nbsp; &nbsp;<td>@Html.DisplayFor(x => x[i].ProductName)</td>&nbsp; &nbsp; &nbsp; &nbsp;<td>@Html.DisplayFor(x => x[i].ProductDes)</td>&nbsp; &nbsp; &nbsp; &nbsp;<td>@Html.DisplayFor(x => x[i].ActiveFlag)</td>&nbsp; &nbsp; &nbsp; &nbsp;<td>@Html.DisplayFor(x => x[i].Price)</td>&nbsp; &nbsp; &nbsp;</tr>}

慕码人2483693

@model List<QBKartMVC.Models.Products>@foreach (var item in Model)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<tr>&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td><input type="checkbox"></td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td>item.ProductCode</td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td>item.ProductName</td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td>item.ProductDes</td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td>item.ActiveFlag</td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td>item.Price</td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</tr>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}

紫衣仙女

你必须创建 IEnumerable List ,像这样@model IEnumerable<QBKartMVC.Models.Products>&nbsp; &nbsp; &nbsp; &nbsp;@foreach (var item in Model)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<tr>&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td><input type="checkbox"></td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td>@Html.DisplayFor(x => x.ProductCode)</td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td>@Html.DisplayFor(x => x.ProductName)</td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td>@Html.DisplayFor(x => x.ProductDes)</td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td>@Html.DisplayFor(x => x.ActiveFlag)</td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td>@Html.DisplayFor(x => x.Price)</td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</tr>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}
打开App,查看更多内容
随时随地看视频慕课网APP