开发者

Execute multiple return one ActionResult

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.

Id开发者_开发百科ea: 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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜