Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/264.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jsf-2/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在MVC as列表中创建动态视图_C#_Asp.net Mvc_Razor - Fatal编程技术网

C# 在MVC as列表中创建动态视图

C# 在MVC as列表中创建动态视图,c#,asp.net-mvc,razor,C#,Asp.net Mvc,Razor,我想创建显示实体属性列表的动态视图 我创建了这些模型 public class PersonModel { public string FirstName { get; set; } public string LastName { get; set; } } public class EmployeeModel : PersonModel { public string CompanyName { get; set; } }

我想创建显示实体属性列表的动态视图

我创建了这些模型

  public class PersonModel
{
    public string FirstName { get; set; }
    public string LastName { get; set; }

}

  public class EmployeeModel : PersonModel
    {
        public string CompanyName { get; set; }

      }

 public class StudentModel : PersonModel
    {
        public string SchoolName { get; set; }

    }
我想要一个显示列表的视图,动态生成的视图 例如,列和数据显示在列表中

打开employee I时的示例将显示以下内容:

当开放学生时,我将展示以下内容:


使视图动态并包含所需列和数据的最简单方法是什么?

我不确定是否正确理解了您的要求,但如果您希望将模型的每个属性动态显示为列标题,则可以尝试以下方法:

在您的视图中,您可以对类型调用GetProperties方法,并为每个属性递归添加一列:

@model PersonModel
@if (Model != null)
{
    <table style="width:100%">
        <tr>
            @foreach (string property in Model.GetType().GetProperties().Select(x => x.Name).ToList())
            {
            <td>@property</td>
            }
        </tr>
    </table>
}
@model PersonModel
@如果(型号!=null)
{
@foreach(Model.GetType().GetProperties()中的字符串属性。选择(x=>x.Name.ToList())
{
@财产
}
}
在填充行之前,可以使用此示例填充表的标题列。要填充这些行,您需要一个PersonModel列表,并在此基础上进行foreach,类似于我为列标题显示的内容


希望这有帮助。

我希望这和我认为的一样有意义

由于
列表
列表
列表
实际上被认为是完全不同的,因此您需要一种方法来克服这个问题。我使用一个通用容器类:

public interface IGenericContainer
{
    dynamic Data { get; }
}

public class GenericContainer<T> : IGenericContainer
{
    T _Data { get; set; }
    public GenericContainer(T data)
    {
        _Data = data;
    }
    dynamic IGenericContainer.Data
    {
        get { return _Data; }
    }
}

public class GenericContainer
{
    public static GenericContainer<T> Create<T>(T data)
    {
        return new GenericContainer<T>(data);
    }
}

PersonList是模型中的一个属性,类型为
List
,或任何模型的列表。

您可以创建两个由不同模型键入的局部视图,然后在视图中使用。可能让你的动作方法根据模型切换,然后返回视图。相当困难。希望我的答案能满足您的需要。太棒了,我喜欢它,有没有办法先加载父类属性,然后加载驱动类,例如上面的employee,我需要名字、姓氏,然后是公司名称?尝试将
GetProperties()
更改为
GetProperties().OrderBy(x=>x.MetadataToken)
如果这个问题不起作用,那么这个问题的答案似乎可以解决这个问题:谢谢,我需要任何型号的总体视图。所以视图现在不能显示这种类型的模型
@using System.Reflection;
@using System.Text;
@{
    Layout = null;
}
@model IGenericContainer
@{
    IEnumerable<PropertyInfo> properties = null;
    if (Model.Data.Count > 0)
    {
        properties = Model.Data[0].GetType().GetProperties();
    }
}
<div>
@if (properties != null)
{
    <table>
        <thead>
            <tr>
                @foreach (var prop in properties)
                {
                    <td>@prop.Name</td>
                }
            </tr>
        </thead>
        <tbody>
            @for (int i = 0; i < Model.Data.Count; i++)
            {
                <tr>
                @foreach (var prop in properties)
                {
                    <td>@prop.GetValue(Model.Data[i])</td>
                }
                </tr>
            }
        </tbody>
    </table>
}
</div>
@Html.DisplayFor(m => GenericContainer.Create(Model.PersonList), "GenericGrid")