I have a View which is displaying in MVC. It includes a partial View which is not appearing. When I use @Html.Partial or @Html.RenderPartial it does not hit my breakpoint in the Controller class. When I use @Html.RenderAction it does hit my breakpoint
and it returns HTML markup, but it displays as angle brackets in the browser (ie. <table><tr> etc.). How should this work? I think Microsoft needs to add about 100 more ways to return information from the Controller because I'm not nearly confused enough.
(joking)
Render Partial and Render Action are very different in their behavior with performance implications to each. Render Partial is just including a view, there is no controller processing that happens and it's very similar to server side includes (albeit more
powerful). RenderAction actually invokes the entire asp.net mvc runtime, meaning routnig, controller action methods and finally the view. RenderAction works well for some situations but overusing it can result in a performance hit.
@Html.Partial will not hit the controller (hence the breakpoint not firing). It simply loads the view directly and displays it inline. This is good for templating situations, but if you need to pull data or do some processing server side, then you need to
call RenderAction.
It sounds like your view is getting html encoded, hence the html showing up in the browser end view. What does your action method look like ? The one being called from RenderAction? Can you post that code? It should just be returning a view as you normally
would.
I want to display multiple webgrids in one view each showing a different recordset from a stored procedure. So I created a view and then a partial view. The view has multiple lines of code that looks like:
@{Html.RenderAction("StoreStandingsList", new { FormCollection = ViewData["FormData"], GroupName = "Wash", Type = "Ticket", Header = "Net Ticket", HeaderClass = "BlueHeader" });}
It calls to a method named StoreStandingsList in the Controller. That method loads some ViewData values, most importantly the Grid. I have to apply a little more processing to the output of the grid's GetHtml method and I prefer to do that in the Controller
so I just return the output of the GetHtml method to the Partial View via the ViewData["Grid"] code snippet above. If you want to suggest a completely different way of approaching this please do, but I don't know if I can understand anything very complicated
because this is my first real MVC and Razor project.
One other thing> I'm using a master page in the View. When I call Html.RenderAction it re-displays the master page header in each Partial View although the master page is not listed in the Partial View.
DallasSteve
Member
222 Points
338 Posts
How To Render A Partial View In Razor
May 01, 2012 05:00 PM|LINK
I have a View which is displaying in MVC. It includes a partial View which is not appearing. When I use @Html.Partial or @Html.RenderPartial it does not hit my breakpoint in the Controller class. When I use @Html.RenderAction it does hit my breakpoint and it returns HTML markup, but it displays as angle brackets in the browser (ie. <table><tr> etc.). How should this work? I think Microsoft needs to add about 100 more ways to return information from the Controller because I'm not nearly confused enough. (joking)
www.SteveGaines.us
CodeHobo
All-Star
18669 Points
2648 Posts
Re: How To Render A Partial View In Razor
May 01, 2012 05:11 PM|LINK
Render Partial and Render Action are very different in their behavior with performance implications to each. Render Partial is just including a view, there is no controller processing that happens and it's very similar to server side includes (albeit more powerful). RenderAction actually invokes the entire asp.net mvc runtime, meaning routnig, controller action methods and finally the view. RenderAction works well for some situations but overusing it can result in a performance hit.
@Html.Partial will not hit the controller (hence the breakpoint not firing). It simply loads the view directly and displays it inline. This is good for templating situations, but if you need to pull data or do some processing server side, then you need to call RenderAction.
It sounds like your view is getting html encoded, hence the html showing up in the browser end view. What does your action method look like ? The one being called from RenderAction? Can you post that code? It should just be returning a view as you normally would.
Blog | Twitter : @Hattan
francesco ab...
All-Star
20954 Points
3286 Posts
Re: How To Render A Partial View In Razor
May 01, 2012 05:13 PM|LINK
@Partial and @RenderPartial do not recall the controller, they just display some information that you already have in the View and pass them.
Mvc Controls Toolkit | Data Moving Plug-in Videos
DallasSteve
Member
222 Points
338 Posts
Re: How To Render A Partial View In Razor
May 01, 2012 05:54 PM|LINK
CodeHobo
It's pretty complicated. Here's the short answer:
Here's the long answer:
I want to display multiple webgrids in one view each showing a different recordset from a stored procedure. So I created a view and then a partial view. The view has multiple lines of code that looks like:
@{Html.RenderAction("StoreStandingsList", new { FormCollection = ViewData["FormData"], GroupName = "Wash", Type = "Ticket", Header = "Net Ticket", HeaderClass = "BlueHeader" });}It calls to a method named StoreStandingsList in the Controller. That method loads some ViewData values, most importantly the Grid. I have to apply a little more processing to the output of the grid's GetHtml method and I prefer to do that in the Controller so I just return the output of the GetHtml method to the Partial View via the ViewData["Grid"] code snippet above. If you want to suggest a completely different way of approaching this please do, but I don't know if I can understand anything very complicated because this is my first real MVC and Razor project.
www.SteveGaines.us
DallasSteve
Member
222 Points
338 Posts
Re: How To Render A Partial View In Razor
May 01, 2012 06:00 PM|LINK
CodeHobo
One other thing> I'm using a master page in the View. When I call Html.RenderAction it re-displays the master page header in each Partial View although the master page is not listed in the Partial View.
www.SteveGaines.us
DallasSteve
Member
222 Points
338 Posts
Re: How To Render A Partial View In Razor
May 01, 2012 08:03 PM|LINK
Problem solved. There were 2 main issues hanging me up.
1 - When returning a Partial View ActionResult in the Controller (at least with Razor) you should use:
otherwise the _ViewStart.cshtml page gets called every time which places the master page in each partial view.
2 - I needed to use Html.Action to return the result as an HTML string:
@Html.Action("GetStoreStandingsList", new { oViewData = ViewData })www.SteveGaines.us