0

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();

}

tereško
  • 58,060
  • 25
  • 98
  • 150
Dave
  • 1,822
  • 2
  • 27
  • 36

1 Answers1

1

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.

Timothy Walters
  • 16,866
  • 2
  • 41
  • 49
  • Timothy, thanks for the input. I have to say I gave up on this approach and went with SignalR after all as it worked really nice for what all I wanted to do! – Dave Sep 10 '13 at 01:27
  • SignalR looks to be a wonderful system, I haven't had a chance to play with it much myself, but many people I trust have said how great it is. – Timothy Walters Sep 10 '13 at 01:56