【发布时间】:2021-05-24 23:19:57
【问题描述】:
我有:
- 具有 2 个泛型的接口
- 实现接口和
PageModel的抽象类 - 实现抽象类的剃须刀页面类
- 需要接口的部分
当我尝试@await Html.PartialAsync("_InterfacePartial", Model) 时,它会抛出以下内容
InvalidOperationException:传递到 ViewDataDictionary 的模型项的类型为“IndexPageModel”,但此 ViewDataDictionary 实例需要类型为“IPage`2[System.Object,IRow]”的模型项。
Microsoft.AspNetCore.Mvc.ViewFeatures.ViewDataDictionary.EnsureCompatible(对象值)
IPage.cs
public interface IPage<T, T2>
where T : class
where T2 : IRow
{
// STRIPPED FOR BREVITY
IQueryable<T> GetQueryable();
IEnumerable<T2> GetResults(Func<T, bool> predicate);
}
AbstractPage.cs
public abstract class AbstractPage<T, T2> : PageModel, IPage<T, T2>
where T : class
where T2 : IRow
{
// STRIPPED FOR BREVITY
public abstract IQueryable<T> GetQueryable();
public IEnumerable<T2> GetResults(Func<T, bool> predicate)
{
var fields = ... // Edit after answer => `var fields = GetTableFields();` returns Func<T, T2>
return GetQueryable().Where(predicate).ToList().Select(fields);
}
}
IndexPageModel.cshtml.cs
public class IndexPageModel : AbstractPage<Thing, ThingRow>
{
public override IQueryable<Order> GetQueryable()
{
return MyDbContext.Things.Include(x => x.AnExtraThing);
}
}
是我做错了什么,还是我只是一个试图做不可能的事的疯子?
【问题讨论】:
标签: c# asp.net asp.net-core razor