user1765862
user1765862

Reputation: 14145

cannot render asp.net mvc partial view

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

Upvotes: 0

Views: 1653

Answers (4)

matt_lethargic
matt_lethargic

Reputation: 2786

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

Upvotes: 2

Caótico fanegas
Caótico fanegas

Reputation: 101

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"; 
    }
}

Upvotes: 1

malkam
malkam

Reputation: 2355

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

Upvotes: 1

Complexity
Complexity

Reputation: 5820

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.

Upvotes: 2

Related Questions