ご存知かもしれませんが、ASP.NET MVC は、ビュー内のモデル フィールドのカスタム ビュー オーバーライドをサポートしています。フォルダには などの特別なフォルダがあり、これらのViewsフォルダにはViews\Shared\EditorTemplatesのViews\Shared\DisplayTemplatesようなファイルを含めることができます。これらのファイルは、フィールドを持つモデルでビューをViews\Shared\EditorTemplates\String.cshtml呼び出すときに使用されるデフォルト ビューをオーバーライドします。@Html.EditorForString
私がやりたいのは、この機能をカスタムの種類のテンプレートに使用することです。Views\Shared\GroupTemplatesegViews\Shared\GroupTemplates\String.cshtmlとを含むようなフォルダーが必要で、 exampleを呼び出すことができるメソッドViews\Shared\GroupTemplates\Object.cshtmlを作成したいと考えています。HtmlHelperHtml.GroupFor(foo => foo.Bar)String.cshtmlBarStringObject.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ように実装する必要がありますか?