How to render MVC child Action PartialView into La

2019-04-13 08:21发布

问题:

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>

回答1:

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.



回答2:

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



回答3:

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