Reputation: 43
Scenario: My app has to make a request (AJAX) and based on request multiple divs (these divs are PartialViews) have to be updated on client. The divs or partialviews can be expensive call if made individually.
Idea: When a request is done, JS will collect list of partial views to be updated with whatever parameters and send AJAX request. Controller receives the request and executes all partial views in parallel using TP library, collect rendered markup, and send back response to client where client plots the partial views/div in page.
Challenge: On the server after receiving list of partial views with parameters. How can I execute partial views (names passed from client) in the Action Method and get their markup/json response?
Thanks in advance.
Upvotes: 2
Views: 454
Reputation: 19733
Using the code from this question to get the HtmlHelper:
Using HtmlHelper in a Controller
public static HtmlHelper GetHtmlHelper(this Controller controller)
{
var viewContext = new ViewContext(controller.ControllerContext, new FakeView(), controller.ViewData, controller.TempData, TextWriter.Null);
return new HtmlHelper(viewContext, new ViewPage());
}
public class FakeView : IView
{
public void Render(ViewContext viewContext, TextWriter writer)
{
throw new InvalidOperationException();
}
}
Then in your controller action method:
var helper = GetHtmlHelper(this);
var html1 = helper.Partial("PartialView1");
var html2 = helper.Partial("PartialView2");
Then return the HTML fragments in whatever format is convenient for you.
Upvotes: 3