ご存知かもしれませんが、ASP.NET MVC は、ビュー内のモデル フィールドのカスタム ビュー オーバーライドをサポートしています。フォルダには などの特別なフォルダがあり、これらのViews
フォルダにはViews\Shared\EditorTemplates
のViews\Shared\DisplayTemplates
ようなファイルを含めることができます。これらのファイルは、フィールドを持つモデルでビューをViews\Shared\EditorTemplates\String.cshtml
呼び出すときに使用されるデフォルト ビューをオーバーライドします。@Html.EditorFor
String
私がやりたいのは、この機能をカスタムの種類のテンプレートに使用することです。Views\Shared\GroupTemplates
egViews\Shared\GroupTemplates\String.cshtml
とを含むようなフォルダーが必要で、 exampleを呼び出すことができるメソッドViews\Shared\GroupTemplates\Object.cshtml
を作成したいと考えています。HtmlHelper
Html.GroupFor(foo => foo.Bar)
String.cshtml
Bar
String
Object.cshtml
予想される動作の完全な例。これが含まれている場合Views\Shared\GroupTemplates\String.cshtml
:
@model String
This is the string template
...そしてViews\Shared\GroupTemplates\Object.cshtml
これが含まれています:
@model Object
This is the object template
私は次のようなモデルを持っています:
class Foo
{
public bool Bar { get; set; }
public String Baz { get; set; }
}
そして、次のViews\Foo\Create.cshtml
ようなビュー:
@model Foo
@Html.GroupFor(m => m.Bar)
@Html.GroupFor(m => m.Baz)
ビューをレンダリングするCreate.cshtml
と、結果は次のようになります。
This is the object template
This is the string template
どのGroupFor
ように実装する必要がありますか?