Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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:

  • /Views/
    • MyLayout.cshtml
    • MyView.cshtml
  • /Shared/
    • _MyPartialView.cshtml

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.

like image 881
Graham Kane Avatar asked Oct 04 '22 03:10

Graham Kane


2 Answers

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

like image 110
Abe Avatar answered Oct 10 '22 02:10

Abe


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.

like image 45
Khalid Abuhakmeh Avatar answered Oct 10 '22 03:10

Khalid Abuhakmeh