asp.NET MVC4 using partial views in partial views

luis12

New Member
Let's assume I have 3 custom controllers: AController, BController and CControler and one default HomeController. Each of the first three controllers have a very unique partial view for them, let's name them APartial, BPartial, CPartial and these are invoked with AFunction, BFunction and CFunction with a template looking like:\[code\]public ActionResult XFunction{ /*put some data into a ViewBag*/ return PartialView("XPartial");}\[/code\]Respectivelly, APartial.cshtml, BPartial.cshtml, CPartial.cshtml are in the \[code\]Shared\[/code\] folder.What is the best way to return all three views in one page when HomeController's Index (ActionResult method) is called. I tried rendering each like:\[code\]using (StringWriter sw = new StringWriter()){ ViewEngineResult viewResult = ViewEngines.Engines.FindPartialView(ControllerContext, "XFunction"); ViewContext viewContext = new ViewContext(ControllerContext, viewResult.View, ViewData, TempData, sw); viewResult.View.Render(viewContext, sw); return MvcHtmlString.Create(sw.GetStringBuilder().ToString());}\[/code\]then putting each into ViewBag's dynamic variables, but I have a feeling there has to be a way nicer solution.
 
Back
Top