0

I need to display all the records in my database to the HTML page.

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

An expression tree may not contain a dynamic operation is the error showing

public IActionResult Index()
        {
            var context = new DBContext();
            return View(context.Products.ToList());
        }

This is the Controller part

Dale K
  • 25,246
  • 15
  • 42
  • 71
  • 1
    And what is the problem? – GvS Aug 26 '19 at 09:30
  • An expression tree may not contain a dynamic operation is the error showing –  Aug 26 '19 at 09:33
  • Maybe the error is in the controller. Can you show the code from the controller? Specially where the query is created. – GvS Aug 26 '19 at 09:41
  • updated the controller section –  Aug 26 '19 at 09:53
  • Possible duplicate of [Razor View Engine : An expression tree may not contain a dynamic operation](https://stackoverflow.com/questions/4155392/razor-view-engine-an-expression-tree-may-not-contain-a-dynamic-operation) – EzLo Aug 26 '19 at 10:55

3 Answers3

0

Update your view code as below,

@model List<QBKartMVC.Models.Products>


@for (var i = 0; i < Model.Count; i++)
{
     <tr>    
       <td><input type="checkbox"></td>
       <td>@Html.DisplayFor(x => x[i].ProductCode)</td>
       <td>@Html.DisplayFor(x => x[i].ProductName)</td>
       <td>@Html.DisplayFor(x => x[i].ProductDes)</td>
       <td>@Html.DisplayFor(x => x[i].ActiveFlag)</td>
       <td>@Html.DisplayFor(x => x[i].Price)</td>
     </tr>
}
Hardik Leuwa
  • 3,282
  • 3
  • 14
  • 28
0

you must create IEnumerable List ,like this

@model IEnumerable<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>
         }
zaman
  • 145
  • 7
0

@model List<QBKartMVC.Models.Products>

@foreach (var item in Model)
            {
                 <tr>    
                    <td><input type="checkbox"></td>
                    <td>item.ProductCode</td>
                    <td>item.ProductName</td>
                    <td>item.ProductDes</td>
                    <td>item.ActiveFlag</td>
                    <td>item.Price</td>
                 </tr>
             }
Nijin P J
  • 1,302
  • 1
  • 9
  • 15