0

I have a partial view display template in Views/DisplayTemplates called Bar.cshtml so that it can be used like:

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

Unfortunately if I want to use this in an action like return PartialView("Bar",fooModel); it won't find it because it's not in the list of folders searched. At the moment I've made a copy of the file and put it in Views/DisplayTemplates as well as Views, but is there a correct way of doing this with the one file?

2 Answers 2

1

Hopefully I've done it now by providing a custom view engine that inherits from the razor view engine and simply adds the view locations to search through:

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();
    }
}
}

And then registered it in Global.asax :

ViewEngines.Engines.Clear();
ViewEngines.Engines.Add(new CustomViewEngine());     
Sign up to request clarification or add additional context in comments.

Comments

0

If you plan to use this in multiple pages you should create a folder in Views/Shared/DisplayTemplates. And to use this template for your FooBar Foo property, decorate it with [UIHint("Bar")] attribute.

3 Comments

This doesn't help because UIHint is doing the same thing as DataType. The exception shows that it's not being looked for: InnerException: System.InvalidOperationException Message=The partial view 'Bar' was not found or no view engine supports the searched locations. The following locations were searched: ~/Views/Home/Bar.aspx ~/Views/Home/Bar.ascx ~/Views/Shared/Bar.aspx ~/Views/Shared/Bar.ascx ~/Views/Home/Bar.cshtml ~/Views/Home/Bar.vbhtml ~/Views/Shared/Bar.cshtml ~/Views/Shared/Bar.vbhtml
Is your Bar.cshtml in Views/DisplayTemplates or in Views/Shared/DisplayTemplates or Views/Home/DisplayTemplates? The one that you specified is not being searched. If you are keeping it inside Views/DisplayTemplates try moving it to /Views/Shared/DisplayTemplates or /Views/Home/DisplayTemplates
It's in Views/Shared/DisplayTemplates, although it doesn't matter where I put it because MVC won't search in /DisplayTemplates, which is the problem

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.