ASP.NET Core 2.1 Partial helper Tag creates issues when Model needs to be loaded from database

asp.net-core entity-framework-core html razor

Question

I keep on getting issues when trying to load a partial view that contains a list of items loaded from a database. I have made an example to illustrate my issue:

I have a class 'Product' for which I have created a View 'Index' which shows a list of products. I would like to load that View into another view (the 'HomePage'). I have used the Tag <Partial name="~/Views/Product/Index.cshtml"/>. I thought I had to add for="Product" or even model="Product" but I keep on getting an error (red curly underlining) with error: An expression tree may not contain a dynamic operation.

Is there a resolution or should I use the old <iframe>-tag instead? I have added the pages below.

THE HOMEPAGE

@using PartialViewAlts.Models;


<div class="row">
    <div class="col-md-12 text-center">
        <h2>Partial View Tester</h2>        
    </div>
</div>
<div class="row">   
    <div>
        <h2>Tag Helper Partial method</h2>       
        <partial name="~/Views/Product/Index.cshtml" for="Product"/>  //produces an error     
    </div> 
</div>

THE PRODUCTS INDEX PAGE

@model IEnumerable<PartialViewAlts.Models.ToDoItem>

<h2>Partial view list of products</h2>
<table class="table">
    <thead>
        <tr>
            <th>@Html.DisplayNameFor(model => model.Name)</th>
            <th>@Html.DisplayNameFor(model => model.Priority)</th>
            <th>@Html.DisplayNameFor(model => model.IsDone)</th>
        </tr>
    </thead>
    <tbody>
        @foreach (var item in Model)
        {
            <tr>
                <td>@Html.DisplayFor(modelItem => item.Name)</td>
                <td>@Html.DisplayFor(modelItem => item.Priority)</td>
                <td>@Html.DisplayFor(modelItem => item.IsDone)</td>                   
            </tr>
        }
    </tbody>
</table>

EDIT: The Home Controller code has been added

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using PartialViewAlts.Data;
using PartialViewAlts.Models;

namespace PartialViewAlts.Controllers
{
    public class HomeController : Controller
    {        
        public IActionResult Index()
        {
            return View();
        }  

     }
}
1
0
1/22/2019 1:26:08 PM

Popular Answer

I believe the issue lies with the fact you're expecting a IEnumerable for model in the partial while you're stating a for="Product". That is a bit unclear as to what it is at this point

Please see: https://docs.microsoft.com/en-us/aspnet/core/mvc/views/tag-helpers/built-in/partial-tag-helper?view=aspnetcore-2.2

it specifically states that

The for attribute assigns a ModelExpression to be evaluated against the current model. A ModelExpression infers the @Model. syntax. For example, for="Product" can be used instead of for="@Model.Product". This default inference behavior is overridden by using the @ symbol to define an inline expression. The for attribute can't be used with the model attribute.

Editing to give an example:

public class IndexModel{
public IEnumerable<Product> Products{get;set;} 

}

The above is an example model you can use. In this case you have IndexModel as the model for your index page while when calling the partial view you will refer as for="@Model.Products"

That way you're passing an IEnumerable inside your partial.

The new error you're getting is because the model you're trying to pass has a mismatch with what the partial method is expecting.

0
1/18/2019 7:05:49 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow