0

次のように使用できるように 、Views/DisplayTemplates呼び出された部分ビュー表示テンプレートがあります。Bar.cshtml

[DataType("Bar")]  
public FooBar Foo {get;set;}

return PartialView("Bar",fooModel);残念ながら、これを検索されたフォルダのリストにないために見つからないようなアクションで使用したい場合。現時点では、ファイルのコピーを作成してViews/DisplayTemplates、同様に配置しましViewsたが、1つのファイルでこれを行う正しい方法はありますか?

4

2 に答える 2

1

うまくいけば、かみそりのビューエンジンを継承し、検索するビューの場所を追加するだけのカスタムビューエンジンを提供することで、これを実行できたと思います。

using System.Linq;
using System.Web.Mvc;

namespace MvcApplication1
{
public class CustomViewEngine : RazorViewEngine
{
    public CustomViewEngine()
        : this(null)
    {

    }

    public CustomViewEngine(IViewPageActivator activator)
        : base(activator)
    {
        var partialViewLocationFormatsList = PartialViewLocationFormats.ToList();

        partialViewLocationFormatsList.Add("~/Views/{1}/DisplayTemplates/{0}.cshtml");
        partialViewLocationFormatsList.Add("~/Views/{1}/EditorTemplates/{0}.cshtml");
        partialViewLocationFormatsList.Add("~/Views/Shared/DisplayTemplates/{0}.cshtml");
        partialViewLocationFormatsList.Add("~/Views/Shared/EditorTemplates/{0}.cshtml");

        PartialViewLocationFormats = partialViewLocationFormatsList.ToArray();

        var areaPartialViewLocationFormatsList = AreaPartialViewLocationFormats.ToList();

        areaPartialViewLocationFormatsList.Add("~/Areas/{2}/Views/{1}/DisplayTemplates/{0}.cshtml");
        areaPartialViewLocationFormatsList.Add("~/Areas/{2}/Views/{1}/EditorTemplates/{0}.cshtml");
        areaPartialViewLocationFormatsList.Add("~/Areas/{2}/Views/Shared/DisplayTemplates/{0}.cshtml");
        areaPartialViewLocationFormatsList.Add("~/Areas/{2}/Views/Shared/EditorTemplates/{0}.cshtml");

        AreaPartialViewLocationFormats = areaPartialViewLocationFormatsList.ToArray();
    }
}
}

そしてそれをGlobal.asaxに登録しました:

ViewEngines.Engines.Clear();
ViewEngines.Engines.Add(new CustomViewEngine());     
于 2012-02-16T19:36:08.083 に答える
0

これを複数のページで使用する場合は、にフォルダを作成する必要がありますViews/Shared/DisplayTemplates。また、このテンプレートをFooBar Fooプロパティに使用するには、[UIHint("Bar")]属性で装飾します。

于 2012-02-16T11:04:24.427 に答える