【问题标题】:ASP.NET MVC Razor Headers and Views for Each Column Dynamically每列的 ASP.NET MVC Razor 标题和视图动态
【发布时间】:2012-12-04 22:37:41
【问题描述】:

我现在有以下 Razor 行:

<table border=1 cellpadding=3 cellspacing=1 rules="rows" frame="box">
<tr>
    <th>Türkçe Söz Dizisi</th>
    <th>English Word Sequence</th>
    <th></th>
</tr>

@foreach (var item in Model) {
<tr>
    <td>
        @Html.DisplayFor(modelItem => item.Tr)
    </td>
    <td>
        @Html.DisplayFor(modelItem => item.En)
    </td>
    <td>
        @Html.ActionLink((String)@ViewBag.Edit, "Edit", new { id=item.ID }) |
        @Html.ActionLink((String)@ViewBag.Delete, "Delete", new { id=item.ID })
    </td>
</tr>
}

</table>

但是,我会自动将一些列从程序添加到数据库表中。我需要一种方法来相应地打印这个 th 和 td。简而言之,我需要一种方法来浏览 Model 的动态列。我怎样才能做到这一点?

编辑:我的模型类型是“提案”。但是,我想达到 Proposal.Type.Word(Tr, En, 或任何其他添加的语言枚举) 的动态属性。我该怎么办?

@foreach (var item in Model) {
    Type objectType = Type.GetType(typeof(RexamOneriSistemi.Models.Word).AssemblyQualifiedName);
    System.Reflection.PropertyInfo[] fields = objectType.GetProperties();

<tr>
    <td>@Html.DisplayFor(modelItem => item.ID)</td>
    <td>@Html.DisplayFor(modelItem => item.Owner.Name)</td>
    @foreach (System.Reflection.PropertyInfo f in fields) {
    if (f.Name.ToString() == @HttpContext.Current.Session["Language"].ToString())
    {
        <td>
         // What to do here exactly to get item.Type.Word.[dynamic attribute]?
        </td>
    }
</tr>
}

我可以得到剃刀字符串

string s = "@Html.Displayfor(modelItem => item.Type.Word." + System.Web.HttpContext.Current.Session["Language"] + ")"

Resulting: @Html.Displayfor(modelItem => item.Type.Word.Tr)

我可以插入要呈现为 Razor 语法的字符串吗?如果是,怎么做?

【问题讨论】:

  • 你的视图是强类型的吗?
  • 是或否,没关系。我可以根据我的问题的任何有效答案进行更改。

标签: asp.net-mvc-3


【解决方案1】:

我试过这段代码,它可以工作

 <table border=1 cellpadding=3 cellspacing=1 rules="rows" frame="box">
    <tr>
      @{
        Type objectType = Type.GetType(typeof(yourProjectNamespace.Models.Language).AssemblyQualifiedName);


        System.Reflection.PropertyInfo[] fields = objectType.GetProperties();

         foreach (System.Reflection.PropertyInfo f in fields)
         { 

         <th> @f.Name.ToString() </th>

       }
    }

    </tr>

    @foreach (yourModelType item in Model) {
    <tr>
    foreach (System.Reflection.PropertyInfo f in fields)
         { 

         <td>@Html.Display(f.Name.ToString())</td>

         }
        <td>
            @Html.ActionLink((String)@ViewBag.Edit, "Edit", new { id=item.ID }) |
            @Html.ActionLink((String)@ViewBag.Delete, "Delete", new { id=item.ID })
        </td>
    </tr>
    }
            </table>

要获得您班级的 Assemply 合格名称,请参阅此链接 here

【讨论】:

  • 你摇滚!还有一件很难的事,您班级的 AssemblyQualifiedName ?例如,类是“语言”。
  • 你好,你确定 Html.Display(f.Name.ToString()) 工作正常吗?我还有一些关于这个话题的问题,编辑了这个问题?你要看看吗?
【解决方案2】:

遇到了这个问题,写了这个也许会对其他人有所帮助。此代码会将您的模型动态加载到表中。

@model System.Collections.Generic.List<App.Client.Models.MyModel>

<table>
    <thead>
        <tr>
            @foreach (var property in Model.GetType().GetGenericArguments()[0].GetProperties())
            {
                <th>@property.Name</th>
            }
        </tr>   
    </thead>
    <tbody>
        @foreach (var modelItem in Model)
        {
            <tr>
                @foreach (var property in modelItem.GetType().GetProperties())
                {
                    <td>@property.GetValue(modelItem)</td>
                }
            </tr>
        }
    </tbody>
</table>

【讨论】:

  • 非常棒。谢谢!
  • 那么 DisplayName() 呢?
猜你喜欢
  • 1970-01-01
  • 2018-04-08
  • 2011-03-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-10-09
  • 1970-01-01
相关资源
最近更新 更多