2

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>
user266909
  • 1,841
  • 3
  • 32
  • 58

3 Answers3

0

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.

@model myApp.viewModel

@{
    Layout = "~/Views/Shared/Layout.cshtml";  // <---- adding the layout 
}

@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>
    }
}

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

David Silva-Barrera
  • 1,006
  • 8
  • 12
Marko
  • 2,734
  • 24
  • 24
  • That creates stack overflow by including the @{Layout="~/Views/Shared/_Layout.cshtml";}. I know @Html.Partial() will insert the fragments at the line where the action is invoked, but I would like it rendered at the line where the @RenderSection("userProfile") is in the layout. – user266909 Nov 22 '13 at 01:02
  • ... and I would like to use @section userProfile{} in the partialView. – user266909 Nov 22 '13 at 01:09
0

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.

Community
  • 1
  • 1
Venkatesh
  • 1,204
  • 1
  • 10
  • 18
0

ok here is how i did it

in the main _ViewStart.cshtml i put this code

@{
    if (Request["Partial"] == null)
    {
        //return full view
        Layout = "~/Views/Shared/_Layout.cshtml";
    }
    else
    {
        //this will give just a partialview with the bodyContent
        Layout = "~/Views/Shared/_LayoutPartial.cshtml";
    }
}

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

CMS
  • 3,657
  • 1
  • 27
  • 46