Asp.net core 如何在具有模型继承的ASP.Net核心Razor页面中使用DisplayTemplates?
试图让ASP.NETCore2.2中的DisplayTemplates处理从类似于此问题的基类继承的类Asp.net core 如何在具有模型继承的ASP.Net核心Razor页面中使用DisplayTemplates?,asp.net-core,display-templates,Asp.net Core,Display Templates,试图让ASP.NETCore2.2中的DisplayTemplates处理从类似于此问题的基类继承的类 PrincipalDisplayTemplate用于列表中的所有项目,我缺少什么 PageModel public class IndexModel : PageModel { public List<Principal> Principals { get; set; } = new List<Principal>(); public void OnG
Principal
DisplayTemplate用于列表中的所有项目,我缺少什么
PageModel
public class IndexModel : PageModel
{
public List<Principal> Principals { get; set; } = new List<Principal>();
public void OnGet()
{
Principals.Add(new Principal { Id = 1, Name = "Principal 1" });
Principals.Add(new UserPrincipal { Id = 1, Name = "User 1", Age = 30 });
Principals.Add(new GroupPrincipal { Id = 1, Name = "Group 1", Members = 5 });
Principals.Add(new UserPrincipal { Id = 1, Name = "User 2", Age = 40 });
Principals.Add(new GroupPrincipal { Id = 1, Name = "Group 2", Members = 3 });
}
}
public class Principal
{
public int Id { get; set; }
public string Name { get; set; }
}
public class UserPrincipal : Principal
{
public int Age { get; set; }
}
public class GroupPrincipal : Principal
{
public int Members { get; set; }
}
@model Principal
<div>
<h4>Principal</h4>
@Model.Name
</div>
@model UserPrincipal
<div>
<h4>User</h4>
@Model.Name, Age @Model.Age
</div>
@model GroupPrincipal
<div>
<h4>Group</h4>
@Model.Name, Members @Model.Members
</div>
~/Pages/Shared/DisplayTemplates/Principal.cshtml
public class IndexModel : PageModel
{
public List<Principal> Principals { get; set; } = new List<Principal>();
public void OnGet()
{
Principals.Add(new Principal { Id = 1, Name = "Principal 1" });
Principals.Add(new UserPrincipal { Id = 1, Name = "User 1", Age = 30 });
Principals.Add(new GroupPrincipal { Id = 1, Name = "Group 1", Members = 5 });
Principals.Add(new UserPrincipal { Id = 1, Name = "User 2", Age = 40 });
Principals.Add(new GroupPrincipal { Id = 1, Name = "Group 2", Members = 3 });
}
}
public class Principal
{
public int Id { get; set; }
public string Name { get; set; }
}
public class UserPrincipal : Principal
{
public int Age { get; set; }
}
public class GroupPrincipal : Principal
{
public int Members { get; set; }
}
@model Principal
<div>
<h4>Principal</h4>
@Model.Name
</div>
@model UserPrincipal
<div>
<h4>User</h4>
@Model.Name, Age @Model.Age
</div>
@model GroupPrincipal
<div>
<h4>Group</h4>
@Model.Name, Members @Model.Members
</div>
@模型主体
校长
@型号.名称
~/Pages/Shared/DisplayTemplates/UserPrincipal.cshtml
public class IndexModel : PageModel
{
public List<Principal> Principals { get; set; } = new List<Principal>();
public void OnGet()
{
Principals.Add(new Principal { Id = 1, Name = "Principal 1" });
Principals.Add(new UserPrincipal { Id = 1, Name = "User 1", Age = 30 });
Principals.Add(new GroupPrincipal { Id = 1, Name = "Group 1", Members = 5 });
Principals.Add(new UserPrincipal { Id = 1, Name = "User 2", Age = 40 });
Principals.Add(new GroupPrincipal { Id = 1, Name = "Group 2", Members = 3 });
}
}
public class Principal
{
public int Id { get; set; }
public string Name { get; set; }
}
public class UserPrincipal : Principal
{
public int Age { get; set; }
}
public class GroupPrincipal : Principal
{
public int Members { get; set; }
}
@model Principal
<div>
<h4>Principal</h4>
@Model.Name
</div>
@model UserPrincipal
<div>
<h4>User</h4>
@Model.Name, Age @Model.Age
</div>
@model GroupPrincipal
<div>
<h4>Group</h4>
@Model.Name, Members @Model.Members
</div>
@model用户主体
使用者
@Model.Name,Age@Model.Age
~/Pages/Shared/DisplayTemplates/GroupPrincipal.cshtml
public class IndexModel : PageModel
{
public List<Principal> Principals { get; set; } = new List<Principal>();
public void OnGet()
{
Principals.Add(new Principal { Id = 1, Name = "Principal 1" });
Principals.Add(new UserPrincipal { Id = 1, Name = "User 1", Age = 30 });
Principals.Add(new GroupPrincipal { Id = 1, Name = "Group 1", Members = 5 });
Principals.Add(new UserPrincipal { Id = 1, Name = "User 2", Age = 40 });
Principals.Add(new GroupPrincipal { Id = 1, Name = "Group 2", Members = 3 });
}
}
public class Principal
{
public int Id { get; set; }
public string Name { get; set; }
}
public class UserPrincipal : Principal
{
public int Age { get; set; }
}
public class GroupPrincipal : Principal
{
public int Members { get; set; }
}
@model Principal
<div>
<h4>Principal</h4>
@Model.Name
</div>
@model UserPrincipal
<div>
<h4>User</h4>
@Model.Name, Age @Model.Age
</div>
@model GroupPrincipal
<div>
<h4>Group</h4>
@Model.Name, Members @Model.Members
</div>
@model-GroupPrincipal
团体
@Model.Name,Members@Model.Members
原因
主体显示模板用于列表中的所有项目
这是因为@Html.DisplayFor(expression)
中的表达式根本不会执行。它们是静态解析的
例如,如果我们有一个表达式m.a.b.c
,其中a
在运行时是null
,那么@Html.DisplayFor(m=>m.a.b.c)
仍然能够知道c
的模板
由于将主体声明为类型列表
,即使创建了模型。主体在运行时持有用户主体
或组主体
,“解析器”仍然将主体
视为基本主体
类型:它们不检查实例的真实类型。它们只是静态地分析类型
如何修复
调用Html.DisplayFor()
时传递模板名称,以便Html助手知道您要使用的真实模板:
@page
@model IndexModel
@foreach (var principal in Model.Principals)
{
@Html.DisplayFor(p => principal, principal.GetType().Name)
}
@页面
@模型索引模型
@foreach(Model.Principals中的var主体)
{
@DisplayFor(p=>principal,principal.GetType().Name)
}
演示
在razor页面中,您是否尝试过@Html.DisplayFor(principal=>principal)?这会生成错误:“名为'principal'的本地或参数不能在此范围内声明,因为该名称在封闭的本地范围内用于定义本地或参数”我过去使用的可能是~/Views/Shared/DisplayTemplates/\u Principle.cshtml
或~/Views/Principals/\u Principle.cshtml
如果您在页面文件夹中使用它,我不确定视图引擎或路由路径是如何配置的,请先在默认路径上尝试,否则您是否在区域内使用它?@sairfan我得到相同的结果,无论是否它们位于“页面”文件夹或“视图”文件夹中。如果将ASP.NET核心Web应用程序项目与.NET核心/框架一起使用,则此操作不起作用。使用ASP.NET Web应用程序(.NET Framework)项目有效。找到此答案,看看这是否有帮助,也可能有帮助