In the layout, MVC child actions are called in . However, the the partialView results are not shown in the RenderSection("userProfile", required:false). The Watch window shows the result contains data, though. Thanks.
Controller's Action
[ChildActionOnly]
public ActionResult GetUserProfile()
{
var vm = base.appVM.User;
return PartialView("UserProfilePartial", vm);
}
UserProfilePartial.cshtml
@model myApp.viewModel
@section userProfile{
<div>@string.Format("{0}, {1}", Model.lastName, Model.firstName)</div>
@foreach (var item in Model.Locations)
{
<div>
<ul class="row">
<li class="cell">@item.LocType</li>
<li class="cell">@item.LocName</li>
<li class="cell">@item.UserRole</li>
</ul>
</div>
}
}
Layout.cshtml
<body>
<header>
<div class="content-wrapper">
<div class="float-left">
<p class="site-title">@Html.ActionLink("Home", "Index", "Home")</p>
</div>
@Html.Action("GetUserProfile","User")
<div class="float-right">
@RenderSection("userProfile", required: false)
</div>
@Html.Action("Index", "Menu");
<div class="menu">
@RenderSection("menu", required:false)
</div>
</div>
</header>
@RenderBody()
</body>
You cannot use sections in partial views. Only the views support using sections. Partial views only meant to display a content not to specify other functions that a function can do. There is similar thread related to it please have a look it at here.
The main issue is that you are rendering a partial view without a layout. When you render the partial view it will only render the code you specify, however you created a section that is not rendered anywhere except the Layout.cshtml however the layout is not invoked anywhere. To fix this you have to add the layout code to your partial view.
After thinking about this i think you should be using @Html.Partial(""); not Render section. Take a look at this link for example http://mvc4beginner.com/Tutorial/MVC-Partial-Views.html
ok here is how i did it
in the main _ViewStart.cshtml i put this code
then i could return the full view and it will return the view baseed on the Partial request (
href=http://www.d.com/account/login?=partial=partial
)it just works!
by the way you could add in the partial layout sections and other partials because after all its only a regular layout