0

Using ajax I'm receiving on asp.net mvc controller action certain string. Based on that string value I want to render partial view.

public ActionResult GetTabData(string activeTab)
        {
            string viewName = String.Empty;
            switch (activeTab)
            {
                case "all":
                    viewName = "_AllPartial";
                    break;
                case "one":
                    viewName = "_OnePartial";
                    break;
                case "two":
                    viewName = "_TwoPartial";
                default:
                    viewName = "_AllPartial";      
                    break;                    
            }               
            return PartialView("/Home/"+viewName);   
        }

All partial views are stored inside Views/Home directory but I'm constantly getting error that partial view cannot be found

The partial view '/Home/_AllPartial' was not found or no view engine supports the searched locations. The following locations were searched:
/Home/_AllPartial
2
  • 2
    Maybe start with tilde: "~/Home/"? Commented Jun 5, 2014 at 13:11
  • You need a custom ViewEngine to do it, see my answer below. Commented Jun 5, 2014 at 13:13

4 Answers 4

2

It's normal because the "Home" directory is not a location where your partial views should be stored.

Partial views should be stored in your /Shared folder to make them work, however, If you want some organization in your project you can always write your own custom ViewEngine.

Here's a sample:

public class ExtendedRazorViewEngine : RazorViewEngine
{
    #region Methods

    public void AddViewLocationFormat(string paths)
    {
        var existingPaths = new List<string>(ViewLocationFormats) {paths};

        ViewLocationFormats = existingPaths.ToArray();
    }

    public void AddPartialViewLocationFormat(string paths)
    {
        var existingPaths = new List<string>(PartialViewLocationFormats) {paths};

        PartialViewLocationFormats = existingPaths.ToArray();
    }

    #endregion
}

So, now in your Global.asax, you need to register this view engine.

 var engine = new ExtendedRazorViewEngine();
 engine.AddPartialViewLocationFormat("~/Views/Grids/{0}.cshtml");
 engine.AddPartialViewLocationFormat("~/Views/Modals/{0}.cshtml");
 ViewEngines.Engines.Add(engine);

In the example above, you see that I create a new engine, and that I specify 2 locations for my views.

This is working in my implementation so give it a try.

Sign up to request clarification or add additional context in comments.

Comments

2

Would this not work?

public ActionResult GetTabData(string activeTab)
    {
        string viewName = String.Empty;
        switch (activeTab)
        {
            case "all":
                viewName = "_AllPartial";
                break;
            case "one":
                viewName = "_OnePartial";
                break;
            case "two":
                viewName = "_TwoPartial";
            default:
                viewName = "_AllPartial";      
                break;                    
        }               
        return PartialView(string.concat("~/Views/Home/", viewName, ".cshtml");   
    }

1 Comment

We need to include file extension also when we specify view name using path like '~Views/Home/Index.cshtml"
1

We need to specify view file name extension(.cshtml/.aspx) also when you specifying directory.

public ActionResult GetTabData(string activeTab)
    {
        string viewName = String.Empty;
        switch (activeTab)
        {
            case "all":
                viewName = "_AllPartial";
                break;
            case "one":
                viewName = "_OnePartial";
                break;
            case "two":
                viewName = "_TwoPartial";
            default:
                viewName = "_AllPartial";      
                break;                    
        }               
        return PartialView("~/Views/Home/"+viewName+".cshtml");   
    }

Comments

1

Place the view in the same view folder as the controller name, or the shared folder, and send the partial view, no "/Home" first. It will automatically resolve the full path to the view.

Also, partials are meant to be rendered inside a parent view. Why are you trying to return it on it's own? Just use a standard view, and to get rid of the layout, just set a different layout depending on your needs:

@{
    if (ViewBag.Modal != null && ViewBag.Modal) 
    { 
        Layout = "~/Views/Shared/_LayoutModal.cshtml";
    }
    else 
    { 
        Layout = "~/Views/Shared/_Layout.cshtml"; 
    }
}

Comments

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.