I have a layout that has a RenderSection. If I attempt to fill this section from my view, all works fine. If I try to fill the section from a partial view, then nothing happens. I don't receive an error message but also no code gets output. This aligns
with what some have said (http://forums.asp.net/p/1632953/4211416.aspx#4211416?Re%3a+Default+content+across+nested+layout) about the nesting of layouts and this just may not be possible. I tried redefining the section within the view, but that did not work
since you can't have a RenderSection within a view in the first place.
So, before I give up, I just wanted confirmation that one can NOT fill a section from a partial view. My workaround would be to fill a TempData value with the output from the partial views that needs to be injected into the section, and then use that in
the view to fill the section. But that just feels dirty. Hoping there is a more elegant method.
Close but not quite. I did not want to have the @section OptionalContent in the view page. I just wanted it in the partial view so that I could further slice and dice what appeared where in the partial views. Not a huge deal, I just had to rearrange how
the page logic was being generated. As you pointed out, the 3rd option in the partial view just won't work.
"And whoever is removed away from the Fire and admitted to Paradise, he indeed is successful." (The Holy Quran)
Excellent Windows VPS Hosting Imran Baloch MVP, MVB, MCP, MCTS, MCPD
thanks, I tried this. The problem is ViewData seems to loose the value ViewData["MainView"] in partial view. So all I get is null and it blows up...any ideas? Is this a threading issue?
thanks, I tried this. The problem is ViewData seems to loose the value ViewData["MainView"] in partial view. So all I get is null and it blows up...any ideas? Is this a threading issue?
Make sure to call the partial view after setting the viewdata. You can also use ViewContext.Controller.ViewData.
"And whoever is removed away from the Fire and admitted to Paradise, he indeed is successful." (The Holy Quran)
Excellent Windows VPS Hosting Imran Baloch MVP, MVB, MCP, MCTS, MCPD
Make sure to call the partial view after setting the viewdata. You can also use ViewContext.Controller.ViewData.
Yes, I did that and tired new suggestion too. but it didnot work. This is how I do it using your suggestion:
@{ ViewData.Add("MainView", this); } @Html.Action("Load", "Home", new { viewname = "_testPartialView"}) @*loads homecontroller and passes references alon to partial view *@
However, I was able to pass page reference in like this instead from layout:
@Html.Action("Load", "Home", new { viewname = "_testPartialView", pageref = this })
The last bit of code will call my Home controller and action Load. I pass in the reference to my partial view from controller. I am now able to get a reference and create a rendersection dynamically. BUT this pattern only holds true when the partial view
is render for the FIRST time. I want to render the same partial view, more than once, like the below:
@Html.Action("Load", "Home", new { viewname = "_testPartialView", pageref = this }) @*we make create rendersection in partialview
@Html.Action("Load", "Home", new { viewname = "_testPartialView", pageref = this }) @*reference no longer valid by the time this renders partialview
What I found is that after I created the rendersection in the layout in my first partialview. When the partialview is called the second time. The reference which I first passed becomes null.
@{
var md = (System.Web.Mvc.WebViewPage)ViewData["MainView"]; @* THIS RETURNS NULL*@ md.DefineSection("Footer", () =>
{
md.WriteLiteral("<div>My Contents</div>");
});
}
To possibly make this work, I must pass the most up to date reference to the next partialview that will be rendered. Any ideas?
RenderSection is different than RenderAction. Calling RenderAction(or Action) will start a brand new request so the ViewData is not shared between them. You can use Context.Items.
"And whoever is removed away from the Fire and admitted to Paradise, he indeed is successful." (The Holy Quran)
Excellent Windows VPS Hosting Imran Baloch MVP, MVB, MCP, MCTS, MCPD
hostdude99
Member
2 Points
3 Posts
RenderSection and PartialViews
Jan 13, 2011 03:31 PM|LINK
I have a layout that has a RenderSection. If I attempt to fill this section from my view, all works fine. If I try to fill the section from a partial view, then nothing happens. I don't receive an error message but also no code gets output. This aligns with what some have said (http://forums.asp.net/p/1632953/4211416.aspx#4211416?Re%3a+Default+content+across+nested+layout) about the nesting of layouts and this just may not be possible. I tried redefining the section within the view, but that did not work since you can't have a RenderSection within a view in the first place.
So, before I give up, I just wanted confirmation that one can NOT fill a section from a partial view. My workaround would be to fill a TempData value with the output from the partial views that needs to be injected into the section, and then use that in the view to fill the section. But that just feels dirty. Hoping there is a more elegant method.
rendersection
raduenuca
All-Star
24675 Points
4250 Posts
Re: RenderSection and PartialViews
Jan 13, 2011 07:42 PM|LINK
Hi,
This is what you try to acomplish?
@{ ViewBag.Title = "Index"; } <h2> Index</h2> @section OptionalContent { @Html.Partial("_OptionalContent") }In _Layout.cshtml
<body> @RenderBody( ) @if ( IsSectionDefined( "OptionalContent" ) ) { @RenderSection( "OptionalContent" ) } else { <div> Default content </div> } </body>As above works fine in MVC3 RTM.
if you try this:
@section OptionalContent { <div> Some extra content </div> }inside the partial view will not work.
Radu Enuca | Blog
hostdude99
Member
2 Points
3 Posts
Re: RenderSection and PartialViews
Jan 14, 2011 03:12 AM|LINK
Close but not quite. I did not want to have the @section OptionalContent in the view page. I just wanted it in the partial view so that I could further slice and dice what appeared where in the partial views. Not a huge deal, I just had to rearrange how the page logic was being generated. As you pointed out, the 3rd option in the partial view just won't work.
Thanks anyway.
imran_ku07
All-Star
45815 Points
7698 Posts
MVP
Re: RenderSection and PartialViews
Jan 14, 2011 04:14 AM|LINK
One way is to do this is to pass the reference of content page to your partial,
@{
ViewData.Add("MainView", this);
}
@{Html.RenderPartial("Partial");}
and Inside your partial define the section as
@{
var md = (System.Web.Mvc.WebViewPage)ViewData["MainView"];
md.DefineSection("Footer", () =>
{
md.WriteLiteral("<div>My Contents</div>");
});
}
Excellent Windows VPS Hosting
Imran Baloch MVP, MVB, MCP, MCTS, MCPD
Ridgeback
Member
43 Points
31 Posts
Re: RenderSection and PartialViews
Nov 30, 2011 10:17 PM|LINK
thanks, I tried this. The problem is ViewData seems to loose the value ViewData["MainView"] in partial view. So all I get is null and it blows up...any ideas? Is this a threading issue?
imran_ku07
All-Star
45815 Points
7698 Posts
MVP
Re: RenderSection and PartialViews
Dec 01, 2011 02:02 AM|LINK
Make sure to call the partial view after setting the viewdata. You can also use ViewContext.Controller.ViewData.
Excellent Windows VPS Hosting
Imran Baloch MVP, MVB, MCP, MCTS, MCPD
Ridgeback
Member
43 Points
31 Posts
Re: RenderSection and PartialViews
Dec 01, 2011 04:31 PM|LINK
Yes, I did that and tired new suggestion too. but it didnot work. This is how I do it using your suggestion:
@{ViewData.Add("MainView", this);
}
@Html.Action("Load", "Home", new { viewname = "_testPartialView"}) @*loads homecontroller and passes references alon to partial view *@
However, I was able to pass page reference in like this instead from layout:
@Html.Action("Load", "Home", new { viewname = "_testPartialView", pageref = this })
The last bit of code will call my Home controller and action Load. I pass in the reference to my partial view from controller. I am now able to get a reference and create a rendersection dynamically. BUT this pattern only holds true when the partial view is render for the FIRST time. I want to render the same partial view, more than once, like the below:
@Html.Action("Load", "Home", new { viewname = "_testPartialView", pageref = this }) @*we make create rendersection in partialview
@Html.Action("Load", "Home", new { viewname = "_testPartialView", pageref = this }) @*reference no longer valid by the time this renders partialview
What I found is that after I created the rendersection in the layout in my first partialview. When the partialview is called the second time. The reference which I first passed becomes null.
@{
var md = (System.Web.Mvc.WebViewPage)ViewData["MainView"]; @* THIS RETURNS NULL*@
md.DefineSection("Footer", () =>
{
md.WriteLiteral("<div>My Contents</div>");
});
}
To possibly make this work, I must pass the most up to date reference to the next partialview that will be rendered. Any ideas?
imran_ku07
All-Star
45815 Points
7698 Posts
MVP
Re: RenderSection and PartialViews
Dec 02, 2011 02:54 AM|LINK
RenderSection is different than RenderAction. Calling RenderAction(or Action) will start a brand new request so the ViewData is not shared between them. You can use Context.Items.
Here is a sample,
// Main View @{ Layout = "~/Views/Shared/_Layout.cshtml"; Context.Items["MainView"] = this; } @Html.Action("Partial") @Html.Action("Partial") // Partial View @{ Layout = null; var md = (System.Web.Mvc.WebViewPage)Context.Items["MainView"]; if(Context.Items["SecDefined"] ==null){ md.DefineSection("Footer", () => { md.WriteLiteral("<div>My Contents</div>"); }); } Context.Items["SecDefined"] = true; }Excellent Windows VPS Hosting
Imran Baloch MVP, MVB, MCP, MCTS, MCPD