MVC 3 Response.Flush does not work

2019-05-11 00:56发布

问题:

I have the next code of view:

@{ Layout = null; }
@Html.Raw(ViewBag.ReportHeader as string)
@{
    Response.Buffer = true;
    Response.Flush();
}
@Html.Raw(ViewBag.ReportBodyAndFoot as string)

In this scenario, I expected the header of the page to be rendered first and then the body with the footer, but the response sent the entire page.

回答1:

I have found the answer:

public ActionResult Index(DateTime from, DateTime to)
{
  PartialView("PartialViews/_ReportHeader").ExecuteResult(ControllerContext);
  Response.Flush();
  ViewBag.Report = new InHouseFarmInFarmOutReportGenerator().GenerateReport(from, to);
  return View();
}