5

私は今のところ次の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>
}

かみそりの文字列を取得できます

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

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

Razor構文としてレンダリングされる文字列を挿入できますか?はいの場合、どのように?

4

2 に答える 2

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>

クラスの Assemply Qualified Name を取得するには、このリンクを参照してください

于 2012-12-18T02:36:26.690 に答える