Graham Kane
Graham Kane

Reputation: 65

ServiceStack Razor: how do I render a Html.Partial view inside a @section?

I am currently unable to get @Html.Partial() to render a view within a @section.

Is this supported in ServiceStack? In my example code below, the first partial (outside of the @section) does get rendered. Inside the @section only the surrounding HTML gets rendered.

My folder structure looks like this:

MyLayout.cshtml looks like this:

@inherits ServiceStack.Razor.ViewPage<MyViewModelBase>
...

@RenderBody()

<div id="sidebar">
    @RenderSection("sidebar")
</div>

MyView.cshtml contains this:

@inherits ServiceStack.Razor.ViewPage<MyViewModel>

@{
    Layout = "MyLayout";
}

@Html.Partial("_MyPartialView")

@section sidebar {
    <h2>Side Bar</h2>

    @Html.Partial("_MyPartialView")

    <p>Some other content</p>
}

The partial view contains nothing but plain HTML.

Upvotes: 5

Views: 1490

Answers (2)

Abe
Abe

Reputation: 6514

I reported the bug on Github a couple of days ago and @mythz commited a fix for the issue.

See my SO post: Service Stack 4.0.15: Razor Partial not outputted inside @section

The good news is the fix is available 4.0.16 (pre-release) via Myget (https://github.com/ServiceStack/ServiceStack/wiki/MyGet)

Here is the issue on Github:

https://github.com/ServiceStack/Issues/issues/60

Upvotes: 2

Khalid Abuhakmeh
Khalid Abuhakmeh

Reputation: 10839

Maybe you can do something like this?

@{ var myPartialView = Html.Partial("_MyPartialView"); }

@section sidebar {
    <h2>Side Bar</h2>

    @myPartialView

    <p>Some other content</p>
}

since Html.Partial just returns a MvcHtmlString you can put it into a variable.

Upvotes: 2

Related Questions