C# 访问MVC中@model作用域之外声明的类型
我创建了一个PersonList类,其中包含不同人群的列表。在我的控制器中,我将把该类传递给视图。我的视图是强类型的,所以我引用该类。但我的视图需要这些组的类型(学生、退休、政治),所以我可以在视图的条件语句中使用它们。我通过在PersonList类中为每个组添加一个对象来解决这个问题,但我觉得这不是最好的方法。请告诉我更好的解决办法 .cs文件C# 访问MVC中@model作用域之外声明的类型,c#,html,asp.net-mvc,C#,Html,Asp.net Mvc,我创建了一个PersonList类,其中包含不同人群的列表。在我的控制器中,我将把该类传递给视图。我的视图是强类型的,所以我引用该类。但我的视图需要这些组的类型(学生、退休、政治),所以我可以在视图的条件语句中使用它们。我通过在PersonList类中为每个组添加一个对象来解决这个问题,但我觉得这不是最好的方法。请告诉我更好的解决办法 .cs文件 公共接口IPerson { } 公共抽象类人物 { 公共字符串名称{get;set;} 公共字符串LastName{get;set;} 公共Int16
公共接口IPerson
{
}
公共抽象类人物
{
公共字符串名称{get;set;}
公共字符串LastName{get;set;}
公共Int16年龄{get;set;}
}
公共阶级政治:个人,个人
{
公共字符串参与方{get;set;}
公共int经验{get;set;}
}
公共班级学生:个人、个人
{
公共字符串主键{get;set;}
公共整数年{get;set;}
}
公共类退休人员:个人,IPerson
{
公共整数{get;set;}
公共字符串{get;set;}
}
公共类人员:IPerson
{
public List allrefered=新列表();
public List AllPeople=新列表();
公立学生AStudent=新生();
公共政治非政治性=新政治();
公共退休人员退休=新退休人员();
}
cshtml文件:
@model conferenceattenders.Models.PersonLists
h6{
显示:内联
}
th{
文本对齐:居中;
}
伊米
纳兹维斯科
威克
Partia
多斯维亚德切尼
基鲁内克
韩国
@foreach(Model.AllPeople中的动态项)
{
@项目名称
@item.LastName
@项目.年龄
@if(item.GetType()==Model.APolitic.GetType())
{
@项目.聚会
@项目.经验
b、 d
b、 d
}
@if(item.GetType()==Model.AStudent.GetType())
{
b、 d
b、 d
@项目.主要
@项目.年份
}
}
首先,您可以通过指令在Razor视图中导入名称空间。您可以在执行@model
指令之前执行此操作。然后,您可以在视图中使用您的类型学生
和政治
其次,要检查项
是否为具体类型,可以使用运算符并查看结果是否为null
。因此,您不需要对实例调用GetType()
。现在,您可以删除AStudent
、APolitic
和areered
实例
另外,代码的两个旁注:
foreach
循环中使用dynamic
。这里不需要动态键入。相反,使用Person
作为item
变量的类型。要使其工作,所有人
必须是列表
类型,而不是列表
。事实上,您根本不需要IPerson
接口,因为其中没有定义任何内容学生
,政治
)。student
的一个实例只是一个学生,那么为什么不调用该类student
?政治也一样@using ConferenceAttendees.Models
@model ConferenceAttendees.Models.PersonLists
<style>
h6 {
display: inline
}
th{
text-align:center;
}
</style>
<div>
<table style="width:100%" align="left">
<tr>
<th>Imie</th>
<th>Nazwisko</th>
<th>Wiek</th>
<th>Partia</th>
<th>Doswiadczenie</th>
<th>Kierunek</th>
<th>Rok</th>
</tr>
@foreach (Person item in Model.AllPeople)
{
var student = item as Student;
var politician = item as Politician;
<tr>
<th>@item.Name</th>
<th>@item.LastName</th>
<th>@item.Age</th>
@if (politician != null)
{
<th>@politician.Party</th>
<th>@politician.Experience</th>
<th>b.d</th>
<th>b.d</th>
}
@if (student != null)
{
<th>b.d</th>
<th>b.d</th>
<th>@student.Major</th>
<th>@student.Year</th>
}
</tr>
}
</table>
</div>
@使用ConferenceAttenders.Models
@模型会议与会者。模型。个人列表
h6{
显示:内联
}
th{
文本对齐:居中;
}
伊米
纳兹维斯科
威克
Partia
多斯维亚德切尼
基鲁内克
韩国
@foreach(Model.AllPeople中的个人项目)
{
var student=作为学生的项目;
var Political=作为政治家的项目;
@项目名称
@item.LastName
@项目.年龄
@if(政治家!=null)
{
@政客,政党
@政治家,经验
b、 d
b、 d
}
@如果(学生!=null)
{
b、 d
b、 d
@学生,主修
@学生,今年
}
}
首先,您可以通过指令在Razor视图中导入名称空间。您可以在执行@model
指令之前执行此操作。然后,您可以在视图中使用您的类型学生
和政治
其次,要检查项
是否为具体类型,可以使用运算符并查看结果是否为null
。因此,您不需要对实例调用GetType()
。现在,您可以删除AStudent
、APolitic
和areered
实例
另外,代码的两个旁注:
foreach
循环中使用dynamic
。这里不需要动态键入。相反,使用Person
作为item
变量的类型。要使其工作,所有人
必须是列表
类型,而不是列表
。事实上,您根本不需要IPerson
接口,因为其中没有定义任何内容学生
,政治
)。student
的一个实例只是一个学生,那么为什么不调用该类student
?政治也一样@using ConferenceAttendees.Models
@model ConferenceAttendees.Models.PersonLists
<style>
h6 {
display: inline
}
th{
text-align:center;
}
</style>
<div>
<table style="width:100%" align="left">
<tr>
<th>Imie</th>
<th>Nazwisko</th>
<th>Wiek</th>
<th>Partia</th>
<th>Doswiadczenie</th>
<th>Kierunek</th>
<th>Rok</th>
</tr>
@foreach (Person item in Model.AllPeople)
{
var student = item as Student;
var politician = item as Politician;
<tr>
<th>@item.Name</th>
<th>@item.LastName</th>
<th>@item.Age</th>
@if (politician != null)
{
<th>@politician.Party</th>
<th>@politician.Experience</th>
<th>b.d</th>
<th>b.d</th>
}
@if (student != null)
{
<th>b.d</th>
<th>b.d</th>
<th>@student.Major</th>
<th>@student.Year</th>
}
</tr>
}
</table>
</div>
@使用ConferenceAttenders.Models
@模型会议与会者。模型。个人列表
h6{
显示:内联
}
th{
文本对齐:居中;
}
@using ConferenceAttendees.Models
@model ConferenceAttendees.Models.PersonLists
<style>
h6 {
display: inline
}
th{
text-align:center;
}
</style>
<div>
<table style="width:100%" align="left">
<tr>
<th>Imie</th>
<th>Nazwisko</th>
<th>Wiek</th>
<th>Partia</th>
<th>Doswiadczenie</th>
<th>Kierunek</th>
<th>Rok</th>
</tr>
@foreach (Person item in Model.AllPeople)
{
var student = item as Student;
var politician = item as Politician;
<tr>
<th>@item.Name</th>
<th>@item.LastName</th>
<th>@item.Age</th>
@if (politician != null)
{
<th>@politician.Party</th>
<th>@politician.Experience</th>
<th>b.d</th>
<th>b.d</th>
}
@if (student != null)
{
<th>b.d</th>
<th>b.d</th>
<th>@student.Major</th>
<th>@student.Year</th>
}
</tr>
}
</table>
</div>