访问MVC中@model作用域外声明的类型

本文关键字:声明 类型 作用域 MVC @model 访问 | 更新日期: 2023-09-27 18:15:30

我创建了一个PersonList类,其中包含不同人群的列表。在控制器中,我将把那个类传递给视图。我的视图是强类型的,所以我引用了那个类。但是我的视图需要这些组的类型(学生,退休,政治),所以我可以在视图中的条件陈述中使用它们。我通过在PersonList类中为每个组添加一个对象来解决这个问题,但我觉得这不是最好的方法。请告诉我更好的解决办法。

cs文件

public interface IPerson
{
}
public abstract class Person
{
    public string Name { get; set; }
    public string LastName { get; set; }
    public Int16 Age { get; set; }
}
public class Politics : Person, IPerson
{
    public string Party { get; set; }
    public int Experience { get; set; }
}
public class Students : Person, IPerson
{
    public string Major { get; set; }
    public int Year { get; set; }
}
public class Retired: Person, IPerson
{
    public int Pension { get; set; }
    public string Hobby { get; set; }
}
public class PersonLists : IPerson
{
    public List<Retired> AllRetired = new List<Retired>();
    public List<IPerson> AllPeople = new List<IPerson>();
    public Students AStudent = new Students();
    public Politics APolitic = new Politics();
    public Retired ARetired = new Retired();
}

cshtml文件:

@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 (dynamic item in Model.AllPeople)
            {
              <tr>
              <th>@item.Name</th>
              <th>@item.LastName</th>
              <th>@item.Age</th> 
                @if (item.GetType() == Model.APolitic.GetType())
                {
                    <th>@item.Party</th>
                    <th>@item.Experience</th>
                    <th>b.d</th>
                    <th>b.d</th>
                }
                @if (item.GetType() == Model.AStudent.GetType())
                {
                    <th>b.d</th>
                    <th>b.d</th>
                    <th>@item.Major</th>
                    <th>@item.Year</th>
                }
              </tr>
            }
    </table>
</div>

访问MVC中@model作用域外声明的类型

首先,你可以通过@using指令在你的Razor视图中导入一个命名空间。您可以在@model指令之前这样做。然后,您可以在视图中使用类型StudentsPolitics

第二,要检查item是否为具体类型,可以使用as运算符,看看结果是否为null。因此,您不需要在实例上调用GetType()。现在您可以删除AStudent, APoliticARetired实例。

还有,代码的两个边注:

  1. 不要在foreach循环中使用dynamic。这里不需要动态类型。相反,使用Person作为item变量的类型。为此,AllPeople必须是List<Person>类型而不是List<IPerson>类型。事实上,你根本不需要IPerson接口,因为你没有在那里定义任何东西。
  2. 我建议不要使用复数形式为您的类(Students, Politics)。Students的一个实例只是一个学生,那么为什么不将类命名为Student呢?Politics也一样。

总而言之,您的视图现在看起来像这样:

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