动态地为每个列的ASP.NET MVC Razor标头和视图

时间:2012-12-17 19:31:06

标签: asp.net-mvc-3

我现在有以下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>

但是,我会自动从程序到数据库表添加一些列。我需要一种方法来打印这个和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>
}

我可以获得Razor String

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

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

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

2 个答案:

答案 0 :(得分:6)

我已经尝试过这段代码并且可以正常使用

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

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

答案 1 :(得分:3)

遇到这个问题,写下这可能会帮助别人。此代码将动态将模型加载到表中。

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