0

私は再帰モデルを持っています。

public class Mapping
{
    public string Value { get; set; }
    public List<Mapping> ChildMappings { get; set; }
}

Mapping クラスの EditorTemplate があります。EditorFor( m => m.ChildMappings) による通常のレンダリングは正常に機能します。

配列パラメーター (2,3) に基づいて、特定の子孫ノードを探したいと思います。(たとえば、Mapping.ChildMappings[2].ChildMappings[3])。

ラムダ内でローカル メソッドを呼び出してみましたが、インデクサーに関するエラーと、テンプレートで実行できないすべてのことについてエラーが発生しました。

@Html.EditorFor(m => RecurseMappings(m.BodyMappings, indexes))

@functions{
    private Mapping RecurseMappings(List<Mapping> mappings, int[] indexes)
    {
        Mapping mapping = new Mapping();

        foreach (int index in indexes)
        {
            mapping = mappings[index];
            if (mapping.ChildMappings == null) { mapping.ChildMappings = new List<RequestMapping>(); }
            mappings = mappings[index].ChildMappings;
        }

        return mappings[mappings.Count - 1];
    }

今のところ、次のように、ハードコードされたネストされたレベルの数を少なくとも容易にするために、スイッチに戻ってきました。

    string[] tokens = Model.ChildIndex.Split(',');
    int[] indexes = Array.ConvertAll<string, int>(tokens, int.Parse);

   switch (indexes.GetLength(0))
    {
        case 1:
            {
                var last = Model.BodyMappings[indexes[0]].ChildMappings.Count - 1;
@Html.EditorFor(m => m.BodyMappings[indexes[0]].ChildMappings[last])
                break;
            }

        case 2:
            {
                var last = Model.BodyMappings[indexes[0]].ChildMappings[indexes[1]].ChildMappings.Count - 1;
@Html.EditorFor(m => m.BodyMappings[indexes[0]].ChildMappings[indexes[1]].ChildMappings[last])
                break;
            }
        case 3:
            {
                var last = Model.BodyMappings[indexes[0]].ChildMappings[indexes[1]].ChildMappings[indexes[2]].ChildMappings.Count - 1;
@Html.EditorFor(m => m.BodyMappings[indexes[0]].ChildMappings[indexes[1]].ChildMappings[indexes[2]].ChildMappings[last])
                break;
            }
        case 4:
            {
                var last = Model.BodyMappings[indexes[0]].ChildMappings[indexes[1]].ChildMappings[indexes[2]].ChildMappings[indexes[3]].ChildMappings.Count - 1;
@Html.EditorFor(m => m.BodyMappings[indexes[0]].ChildMappings[indexes[1]].ChildMappings[indexes[2]].ChildMappings[indexes[3]].ChildMappings[last])
                break;
            }
        case 5:
            {
                var last = Model.BodyMappings[indexes[0]].ChildMappings[indexes[1]].ChildMappings[indexes[2]].ChildMappings[indexes[3]].ChildMappings[indexes[4]].ChildMappings.Count - 1;
@Html.EditorFor(m => m.BodyMappings[indexes[0]].ChildMappings[indexes[1]].ChildMappings[indexes[2]].ChildMappings[indexes[3]].ChildMappings[indexes[4]].ChildMappings[last])
                break;
            }
        default:
            break;
    }

それで、失敗した最初の試みのように、パッケージで作業していることをまとめる方法はありますか? ラムダ コード ブロックも試しましたが、式ツリーに変換できないため拒否されました。

4

1 に答える 1

1

おそらくこれは意図した方法ではありませんが、選択したいオブジェクトの新しい Html-Helper を作成すると機能します。

@{  
    var dd =  new ViewDataDictionary<Mapping>(RecurseMappings(Model.BodyMappings, indexes));
    var dc = new MyDataContainer() { ViewData = dd };
    var help = new HtmlHelper<Mapping>(ViewContext, dc);
}

@help.EditorFor(m => m)

インターフェイスを実装するクラスが見つからなかったため、ソース ファイルにカスタム データ コンテナーの定義が含まれています。

public class MyDataContainer : IViewDataContainer
{
    public ViewDataDictionary ViewData { get; set; }
}
于 2012-03-15T19:37:47.563 に答える