Reputation: 5224
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>
Upvotes: 0
Views: 9293
Reputation: 2924
In your first partial view:
I also would recommend to rename your partial view to something not containing the word "Layout" to avoid the mismatch between the view types.
Upvotes: 2
Reputation: 239260
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()
.
Upvotes: 1