How can I add output to an ASP.NET view after the main portions of the view have been displayed? Sort of "real time" if you will. The following sort of works, in that I get each line of output immediately between sleeps, but outputs all the Response.Write test at the top of the page, rather than at the end as part of the already painted view within the overall layout. And I don't get the main View data painted until after the messages. I have looked at similar questions and SignalR seems overkill and I don't need to poll, I just want to add some immediate output as I come up with it during what normally takes about 30 seconds, but would like to paint the rest of the View first so the output is being appended to the end as it is generated. Ideas appreciated. Thanks.

Here is my Controller:

    [HttpGet]
    public ActionResult MyView()
    {
        return View();
    }

Here is my View:

@{Layout = "~/Views/Shared/_SplitterLayout.cshtml";}

@using (Html.BeginForm())
{
  <div class="Clear">
      <p>This rest of this page should show loading data from Excel as it happens.</p>
  </div>
  <div>
      @Html.Partial("MyPartialView")
  </div>
}

Here is my PartialView:

@{
Response.Flush();
Response.Write("<br/>Started.");
Response.Flush();

Response.Write("<br/>Waiting 2 seconds...");
Response.Flush();
System.Threading.Thread.Sleep(2000);

Response.Write("<br/>Waiting 2 seconds...");
Response.Flush();
System.Threading.Thread.Sleep(2000);

Response.Write("<br/>Finished.");
Response.Flush();

}

有帮助吗?

解决方案

Output as much of the view as you can immediately.

Use AJAX to ask for the long-loading data and write the output using JavaScript client-side.

There are plenty of frameworks that can help you do this. You can even have the AJAX request load a partial view and just write the response to the contents of a div.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top