【问题标题】:Create dynamic view in MVC as list在 MVC 中创建动态视图作为列表
【发布时间】:2016-01-07 06:56:47
【问题描述】:

我想制作显示实体属性列表的动态视图。

我创建这些模型

  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; }

    }

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

打开员工时的示例,我将显示以下内容:

当打开学生时,我将显示以下内容:

让我的视图动态化并包含我想要的列和数据的最简单方法是什么?

【问题讨论】:

  • 您可以创建两个由不同模型键入的局部视图,然后在您的视图中使用它。可能让您的操作方法切换每个模型并返回到视图。
  • 相当困难。希望我的回答能满足您的需要。

标签: c# asp.net-mvc razor


【解决方案1】:

我希望这和我想的一样有意义!

由于List<PersonModel>List<EmployeeModel>List<StudentModel> 实际上被认为是完全不同的,你需要一种方法来克服这个问题。我使用通用容器类:

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);
    }
}

然后您需要一个使用它的通用视图。把它放在 Shared/DisplayTemplates/GenericGrid.cshtml

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

而且 PersonList 是您的模型中的一个属性 List&lt;PersonModel&gt; 或您的任何模型的列表。

【讨论】:

  • 太棒了,我喜欢它,有没有办法加载父类属性然后是驱动类,例如上面的员工我需要名字,姓氏,然后是公司名称?
  • 尝试将GetProperties()更改为GetProperties().OrderBy(x =&gt; x.MetadataToken)
  • 如果这不起作用,那么这个问题的答案似乎可以解决问题:stackoverflow.com/questions/358835/…
【解决方案2】:

我不确定我是否正确理解了您的要求,但如果您想将模型的每个属性动态显示为列标题,那么您可以尝试以下操作:

在你看来,你可以在类型上调用 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>
}

您可以使用此示例在填充行之前填充表的标题列。要填充行,您需要一个 PersonModel 列表并对此进行 foreach,类似于我为列标题向您展示的内容。

希望对您有所帮助。

【讨论】:

  • 谢谢,我需要任何型号的通用视图。所以现在不能查看那种类型的模型
猜你喜欢
  • 1970-01-01
  • 2013-07-06
  • 2013-07-17
  • 1970-01-01
  • 1970-01-01
  • 2014-09-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多