How To Render A Partial View In Razorhttp://forums.asp.net/t/1798925.aspx/1?How+To+Render+A+Partial+View+In+RazorTue, 01 May 2012 20:03:47 -040017989254960315http://forums.asp.net/p/1798925/4960315.aspx/1?How+To+Render+A+Partial+View+In+RazorHow To Render A Partial View In Razor <p>I have a View which is displaying in MVC.&nbsp; It includes a partial View which is not appearing.&nbsp; When I use @Html.Partial or @Html.RenderPartial it does not hit my breakpoint in the Controller class.&nbsp; 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. &lt;table&gt;&lt;tr&gt; etc.).&nbsp; How should this work?&nbsp; I think Microsoft needs to add about 100 more ways to return information from the Controller because I'm not nearly confused enough.&nbsp; (joking)</p> <p>&nbsp;</p> 2012-05-01T17:00:56-04:004960327http://forums.asp.net/p/1798925/4960327.aspx/1?Re+How+To+Render+A+Partial+View+In+RazorRe: How To Render A Partial View In Razor <p>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.</p> <p>@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.</p> <p>It sounds like your &nbsp;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.&nbsp;</p> 2012-05-01T17:11:35-04:004960332http://forums.asp.net/p/1798925/4960332.aspx/1?Re+How+To+Render+A+Partial+View+In+RazorRe: How To Render A Partial View In Razor <p>@Partial and @RenderPartial do not recall the controller, they just display some information that you already have in the View and pass them.</p> 2012-05-01T17:13:13-04:004960382http://forums.asp.net/p/1798925/4960382.aspx/1?Re+How+To+Render+A+Partial+View+In+RazorRe: How To Render A Partial View In Razor <p>CodeHobo</p> <p>It's pretty complicated.&nbsp; Here's the short answer:</p> <pre class="prettyprint">@Html.ViewData[&quot;Grid&quot;]</pre> <p>Here's the long answer:</p> <p>I want to display multiple webgrids in one view each showing a different recordset from a stored procedure.&nbsp; So I created a view and then a partial view.&nbsp; The view has multiple lines of code that looks like:</p> <pre class="prettyprint">@{Html.RenderAction("StoreStandingsList", new { FormCollection = ViewData["FormData"], GroupName = "Wash", Type = "Ticket", Header = "Net Ticket", HeaderClass = "BlueHeader" });}</pre> <p>It calls to a method named StoreStandingsList in the Controller.&nbsp; That method loads some ViewData values, most importantly the Grid.&nbsp; 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[&quot;Grid&quot;] code snippet above.&nbsp; 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.</p> <p><br> <br> &nbsp;</p> 2012-05-01T17:54:28-04:004960396http://forums.asp.net/p/1798925/4960396.aspx/1?Re+How+To+Render+A+Partial+View+In+RazorRe: How To Render A Partial View In Razor <p>CodeHobo</p> <p>One other thing&gt; I'm using a master page in the View.&nbsp; 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.</p> <p>&nbsp;</p> 2012-05-01T18:00:37-04:004960523http://forums.asp.net/p/1798925/4960523.aspx/1?Re+How+To+Render+A+Partial+View+In+RazorRe: How To Render A Partial View In Razor <p>Problem solved.&nbsp; There were 2 main issues hanging me up.&nbsp;</p> <p>1 - When returning a Partial View ActionResult in the Controller (at least&nbsp;with Razor) you should use:</p> <pre class="prettyprint">return PartialView();</pre> <p>otherwise the _ViewStart.cshtml page gets called every time which places the master page in each partial view.</p> <p>2 - I needed to use Html.Action to return the result as an HTML string:</p> <pre class="prettyprint">@Html.Action("GetStoreStandingsList", new { oViewData = ViewData })</pre> <p>&nbsp;</p> 2012-05-01T20:03:47-04:00