asp.net-mvc-3 - ASP.NET MVC Razor 标题和每列动态 View

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

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

编辑:我的模型类型是“提案”。但是,我想达到 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 s = "@Html.Displayfor(modelItem => item.Type.Word." + System.Web.HttpContext.Current.Session["Language"] + ")"

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

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

最佳答案

我已经试过这段代码,它的工作原理

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

要获得您的类(class)的装配合格名称,请参阅此链接 here

关于asp.net-mvc-3 - ASP.NET MVC Razor 标题和每列动态 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13920834/

相关文章:

asp.net-mvc-3 - 使用 Razor 在 Asp.Net MVC3 上合并、缩小和 GZIP 样式和脚本的完整解决方案

asp.net-mvc - ASP.NET MVC 3 为托管在一台本地服务器上的多门户应用程序提供防伪造保护

css - MVC3 Razor 样式表内部区域

asp.net - CaSTLe Windsor Controller 工厂和存储库未解析

javascript - 为什么没有触发超链接上的点击事件?

asp.net - 预编译 View : can I still read the contents at runtime?

jquery - Razor mvc3 + jquery + Url 操作 + 部分 View

c# - 使用 Razor 在javascript中调用带有参数的C#代码方法

c# - ASP.NET MVC3 如何直接从 Controller 引用 View

c# - 不同地区不同认证方式