0

In a partial view I have:

@Html.Partial("Page1")

This renders my Page1.cshtml view in the right bit.

But I would like this to be replaced with something that works like the RenderBody on my _Layout page.

@RenderBody()

In my layout file, the "@RenderBody" is filled with whatever View is returned after clicking on an @Html.ActionLink - so how can I emulate the same behaviour as RenderBody in a partial view as opposed to a layout? - it is my understanding that you can't use RenderBody on a partial view.

What I really want is so that when I click on @Html.ActionLink("Page2", "Page2", "Menu", null, null) the section that was @Html.Partial("Page1") becomes @Html.Partial("Page2")

why does this seem so easy on the layout page but more work on a "nested" partial view?


I tried to set my "Layout" as the index of the "Menu" page (which has a "Menu" controller too) but this didn't work:

@{
    Layout = "~/Views/Menu/Index.cshtml";
}
<div class="container body-content">
    @RenderBody()
</div>
jamheadart
  • 5,047
  • 4
  • 32
  • 63
  • So you want to load content dynamically on a click event? – TheBinaryGuy Feb 11 '19 at 13:37
  • Yeah, the content comes from a specific View.cshtml file - e.g. Page1, Page2 - I can do this really easy on a _Layout file in default MVC project but trying to apply the same logic on an already nested partial view doesn't work. – jamheadart Feb 11 '19 at 13:40
  • 1
    Have you considered using jQuery? https://stackoverflow.com/questions/29142422/rendering-partial-view-on-button-click-in-asp-net-mvc – TheBinaryGuy Feb 11 '19 at 13:44
  • To be honest, I had seen that and wondered why it was even necessary. It's SO simple in Layout->Partial but seems so convoluted in Partial->Partial – jamheadart Feb 11 '19 at 13:49
  • 1
    It's necessary because you want to have client side interaction and ASP.Net is a server side framework, so either you'll have to reload whole page when the user interacts and render content based on what user clicked on or you'll have to use something like AJAX if you don't want to reload the whole page. – TheBinaryGuy Feb 11 '19 at 14:06
  • Ah ok, that makes sense, thanks! I guess I'll look into using jQuery now I know the reason WHY. – jamheadart Feb 11 '19 at 14:08
  • Just went for using the ViewBag as a variable to put in the RenderPartial! – jamheadart Feb 16 '19 at 07:31
  • 1
    Yup, that works too (at least to render conditional blocks), but it would still require a full page reload right? – TheBinaryGuy Feb 21 '19 at 14:01
  • Yeah, it's not great but I think for my purposes it's such a quick fix - I know it's not ideal so I will set time aside to master the jQuery / AJAX techniques. I had some working but I didn't fully understand them so I didn't dare to leave them in :/ – jamheadart Feb 21 '19 at 16:26
  • Yup, that seems fair.. – TheBinaryGuy Feb 22 '19 at 15:33

0 Answers0