0

I am getting an error ("Server Error in '/' Application") when I nest partial views in my MVC app. The views work fine individually, but not when nested. It's my understanding that it's okay to do this, so what am I doing wrong?

Essentially, I am trying to use partials as sub-layouts within my _Layout.cshtml.

Here's my main layout - _Layout.cshtml

<!DOCTYPE html>
<html>
<head>...</head>
<body style="padding-top: 80px;">  
    <div class="container-fluid">
        <div class="row">
            <div id="myTab" class="col-lg-12 col-md-12 col-sm-12">
                ...
                <div class="tab-content">
                    <div class="tab-pane fade" id="search">
                       @Html.Partial("~/Areas/Search/Views/Shared/_SearchLayout.cshtml")
                    </div>                   
                </div>
            </div>
        </div>
    </div>
    @RenderBody()
    @RenderSection("scripts", required: false)
</body>
</html>

This is the first partial view (_SearchLayout). If I remove the partials AND @RenderBody, no error.

<div class="container-fluid">
        @Html.Partial("_PolicySearch")
        @Html.Partial("_ClaimSearch")       
    </div>
@RenderBody()

This partial view is nested in the first partial view (_SearchLayout):

 <div class="row top-buffer search-outline form-horizontal">
        <div class="col-md-1 search-icon-size text-primary">
            <i class="glyphicon glyphicon-heart"></i>
        </div>
        <div class="col-md-1 search-icon-size text-primary">
            <h4>Claim Search</h4>
        </div>    
    </div>
2
  • in your controller for the partials do you by chance use return View() rather than return PartialView() Commented Jan 7, 2015 at 19:44
  • I am not using a controller for these partial views, they are shared. Commented Jan 7, 2015 at 20:09

3 Answers 3

2

In your first partial view:

  1. Remove RenderBody
  2. Replace Html.Partial to Html.RenderPartial

I also would recommend to rename your partial view to something not containing the word "Layout" to avoid the mismatch between the view types.

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

1 Comment

There is zero difference between using Html.Partial and Html.RenderPartial, at least as far as this question is concerned. Anecdotal evidence suggests that Html.RenderPartial may be slightly more performant, but functionally they're exactly the same.
1

Use Html.RenderPartial instead

6 Comments

Where are the partial views located? You need to write the path in the same way as the first one @{ Html.RenderPartial("~/Areas/Search/Views/BLAH/BLAH/_PolicySearch")}
And realize that you need to call it whitin braces @{ Html.RenderPartial("ViewName"); }
They are all in the same folder. I tried this - @{Html.RenderPartial("~/Areas/Search/Views/Shared/_PolicySearch.cshtml")} and this @{Html.RenderPartial("_PolicySearch")}. Neither one works.
You missed this @{Html.RenderPartial("~/Areas/Search/Views/Shared/_PolicySearch")}
Tried that and no help. Thanks
|
1

The problem is @RenderBody(). This can only be called in a layout, which when used in this way _SearchLayout.cshtml is not, despite its name.

The important thing to remember about layouts, partials and views in ASP.NET MVC is that they're all views. The only thing that differentiates them is how they're used. In this instance, you're using the _SearchLayout.cshtml view as a partial, and partials can't use @RenderBody().

4 Comments

This is what I thought, but when I remove the @RenderBody() and leave the partial views, I still get the error.
Then there's some other problem at play. You should be getting a yellow screen of death with a stacktrace assuming this is happening in development. If you're in production, you should use something like ELMAH to log errors for you so you can still see the stacktraces there. Alternatively, if you have Visual Studio 2013, you can do remote debugging. See: msdn.microsoft.com/en-us/library/y7f5zaaa.aspx. Short of that, you can always just add <customErrors mode="Off" /> temporarily to your Web.config, but that can be kind of dangerous in production.
No yellow screen of death and Elmah responds with "No errors found". Fiddler shows a 302 response "<h2>Object moved to <a href="/Error/?aspxerrorpath=/">here</a>.</h2>" followed by a 500. I'm very confused.
Is this only occurring in production, or do you see the behavior in development as well? If you can replicate it in development debug and use breakpoints to try to pinpoint the failure (add one right before the partial call, then inside the partial, etc.). Keep going until you can find the exact line where all hell breaks loose.

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.