0

I have a complex type License as a view model.

public class License
{
    public string Name { get; set; }

    //  Other Properties

    public List<Function> Functions { get; set; }
}

public class Function
{
    public string Name { get; set; }

    //  Other Properties

    public List<Unit> Units { get; set; }
}

public class Unit
{
    public string Name { get; set; }

    //  Other Properties
}

Both the Function's view template and Unit's view template are dynamiclly rendered. So the html looks like this:

<!-- LicenseView -->
@model License

@Html.TextBoxFor(m => m.Name)  //  this is OK

@for(int i=0; i<Model.Functions.Count; i++)
{
    @Html.Partial(Model.Functions[i].Name, Model.Functions[i])
}

and the FunctionView may look like this

@model Function

@Html.TextBoxFor(m => m.Name)  //  the generated html element's name is just 'Name'

@for(int i=0; i < Model.Units.Count; i++)
{
    @Html.Partial(Model.Units[i].Name, Model.Units[i])
}

and this is UnitView

@model Unit

@Html.TextBoxFor(m => m.Name)  // the generated html element's name is just 'Name'

So my question is, what should I do the make the Name attribute correct?

Thanks a lot

4

4 に答える 4

1

上記のコードで行う必要がある唯一の変更は、部分ビューの代わりにエディターを使用することです。したがって、基本的にすべてのコードは次のようになります。

@model License

@Html.TextBoxFor(m => m.Name) 
// Editor will take care of the repetition and u don't need to explicitly pass in the name
// Since the model already have the attribute
@Html.EditorFor(Model.Functions)

次に、"Shared" フォルダーの下にエディター テンプレート フォルダー "EditorTemplates" を作成し、ビュー ファイルに "Function" という名前を付けます。

Unit クラスについても同じことを行うと、必要なものが得られます。

于 2012-12-04T12:19:15.787 に答える
0

DisplayName問題を推測することを許してください、しかしあなたは属性を求めていますか?

HTMLヘルパーがフィールドラベルを表示する方法を定義します

public class License
{
    [DisplayName("License Name")]
    public string Name { get; set; }

    //  Other Properties

    public List<Function> Functions { get; set; }
}

public class Function
{
    [DisplayName("Fun Name")]
    public string Name { get; set; }

    //  Other Properties

    public List<Unit> Units { get; set; }
}

public class Unit
{
    [DisplayName("Unit Name")]
    public string Name { get; set; }

    //  Other Properties
}

必ず持っている

using System.ComponentModel;

モデルコードで。

于 2012-12-04T05:56:50.200 に答える
0

@Jack が言ったように、PartialViews の代わりに Editors を使用してこれを行うことができます。

しかし...本当にPartialViewsを使用したい場合は、それを行うことができますが、渡すモデルはトップのもの(ライセンス)でなければなりません。この方法は、David Jessee が提案した方法と似ていますが、1 つのビューをいくつかに分割しています。

于 2012-12-04T12:35:14.640 に答える
0

複雑なオブジェクト グラフのすべての入力を作成し、モデル バインダーによってグラフ全体を再構成できるようにする場合、最も簡単な方法は、グラフ全体をレンダリングする単一のビューまたは部分ビューを作成することです。

@for(int i=0;i<Functions.Length;i++){
    @for(int j=0;j<Units.Length;j++){

        @Html.EditorFor(Functions[i].Length[j].Unit)

    }
}

もう 1 つのオプションは、要素のインデックスをオブジェクト グラフの各リーフの部分ビューに渡す方法を見つけることです。

確かに、単一のビュー内で複雑なモデルをレンダリングするという考えを好まない人はたくさんいます。ただし、他のオプションは、ユニットなどの小さな子ビューを、コンテキストによって注入または提供される追加データに依存させることです。1 つのうち 6 つ、もう 1 つの半ダース。オブジェクト グラフのタイプごとに 1 つのビューまたは部分ビューを作成するという「学問的に正しい」アプローチを行うたびに、最初から再利用できず、唯一の利点であるビューが大量に作成されることになりました。私が得たのは、「見てください!たくさんの小さなファイルがあり、相互に完全に依存しています...なぜそんなことをしたのですか?」と言うことができました。

于 2012-12-04T11:57:30.137 に答える