0

I have a asp.net mvc4 view which includes some partial views. This view also contains a submit button to filter some elements in the grid, see below:

Configure.cshtml

<div id="MyDiv">
    @Html.Partial("../Grids/_CompGrid")
</div>

 @using (Ajax.BeginForm("Search", "Component", ...)
 {
     <input type="submit" name="_search" value="@Resource.CaptionComponentApplyFilter" />
 }

ComponentController.cs

    public PartialViewResult Search()
    {
        // Do some stuff

        return PartialView("_CompGrid");
    }

When returning the partial view above indicated it crashes. It seems like it is not handling the correct path to the partial view. See below message error:

The partial view '_CompGrid' was not found or no view engine supports the searched locations.
The following locations were searched:
~/Views/Component/_CompGrid.aspx
~/Views/Component/_CompGrid.ascx
~/Views/Shared/_CompGrid.aspx
~/Views/Shared/_CompGrid.ascx
~/Views/Component/_CompGrid.cshtml
~/Views/Component/_CompGrid.vbhtml
~/Views/Shared/_CompGrid.cshtml
~/Views/Shared/_CompGrid.vbhtml

Below the directory structure overview of the above files indicated.

/root
  |
  |__ Controllers
  |       |
  |       |__ ComponentController.cs
  |
  |__ Views
  |       |
  |       |__ Home
  |       |     | 
  |       |     |__ Configure.cshtml
  |       |
  |       |__ Grids
  |       |     |
  |       |     |__ _CompGrid.cshtml
  |       |
  |       |  

Any ideas on how can I solve this?

SOLUTION:

Replace below return line in the function:

    public PartialViewResult Search()
    {
        // Do some stuff

        return PartialView("_CompGrid");
    }

by:

return PartialView("../Grids/_CompGrid");

but anyway if someone has a better idea, it will be welcome.

tereško
  • 58,060
  • 25
  • 98
  • 150
Willy
  • 9,848
  • 22
  • 141
  • 284
  • It seems I have just solved it by replacing the returning line by: return PartialView("../Grids/_CompGrid"); but If someone have any better idea, please make suggestion. – Willy Nov 07 '13 at 17:34

1 Answers1

0

You have two options really. Firstly, you could simply reference the partial using ~/Views/Shared/_CompGrid.cshtml. That will always work from the root of the Views folder, so it'll be more obvious how to fix it if your folder structure gets changed.

Your other option would be to put the partial into the ~/Views/Shared/ folder, because it is one of the default search locations when MVC is attempting to locate a view. (You can actually see this from the error you provided above.) So, assuming you moved _CompGrid.cshtml to be located at ~/Views/Shared/_CompGrid.cshtml, the following code would locate your view correctly:

ComponentController:

public PartialViewResult Search()
{
    // Do some stuff

    return PartialView("_CompGrid");
}

Configure.cshtml:

<div id="MyDiv">
    @Html.Partial("_CompGrid")
</div>
John H
  • 14,422
  • 4
  • 41
  • 74
  • 1
    One further note, `Html.RenderPartial` is better, performance-wise, when compared with `Html.Partial`. This is also true of `Html.RenderAction` vs `Html.Action`. See [here](http://5pm.zwares.com/post/1172669451/asp-net-mvc-performance-html-renderpartial-vs) for details. – John H Nov 07 '13 at 18:07
  • Many thanks for the comment about RenderPartial and RenderAction: it's great! I would like to mention that the syntax of Html.RenderPartial must be between enclosed with {} and using a semicolon, for example, in my case: @{Html.RenderPartial("../Grids/_CompGrid");} See below: http://stackoverflow.com/questions/6980823/html-renderpartial-syntax-with-razor and also for differences between them see here as well: http://stackoverflow.com/questions/2729815/what-is-the-difference-if-any-between-html-partialview-model-and-html-rende/2729851#2729851 – Willy Nov 08 '13 at 09:48
  • your first solution does not work: I have replaced return ('../Grids/_CompGrid') by return ('~Views/Grids/_CompGrid') and below error is thrwon: + [System.InvalidOperationException] {"The partial view '~Views/Grids/_CompGrid' was not found or no view engine supports the searched locations. The following locations were searched:\r\n~Views/Grids/_CompGrid"} System.InvalidOperationException – – Willy Nov 08 '13 at 09:53
  • @user1624552 Sorry, I forgot the `.cshtml.` must be included when using the `~`. Also, I was missing the first `/` too. Updated to correct those. – John H Nov 08 '13 at 13:47