Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Async Actions in ASP.NET MVC 4

I have a synchronous call which I need to convert to async, Im using the async/await key words but this only returns once the task has complete what I need to do is return the results to the UI one by one.

The scenario is I have a task list displayed to the user once they have authenticated however I would like the task to be loaded one at a time once they have been retrieved from the DB, here is my actionResult which puts together the search criteria to pre-filter the tasks:

public async Task<ActionResult> Index(string searchTerm = null, int page = 1)
{
    Shared.InitialiseCriteria(SearchCriteria);
    SearchCriteria.DepartmentID = DepartmentID;
    SearchCriteria.PageSize = 15;
    SearchCriteria.FreeText = searchTerm;

    var model = await DoStuff(SearchCriteria);

        if (Request.IsAjaxRequest())
        {
            return PartialView("_ConversationList", model);
        }
    return View(model);

 }

And here is the await task this calls GetConversation which essentially gets the tasks when the first task is found I need it to be loaded to the Index view :

private async Task<Result> DoStuff(CSearchCriteria SearchCriteria)
{

   return GetConversations(SearchCriteria);
}
like image 608
user2711252 Avatar asked Oct 03 '22 03:10

user2711252


1 Answers

The only purpose of async controller in ASP.NET MVC is to free IIS thread to manage some other requests while async operation is in progress. From caller perspective it is the same as if you are using sync controller.

So, I don't think that you can achieve what you want with async controller (you can use it but it will not solve your problem).

I think you can implement some sort of paging at server-side to retrieve portions of data and send it to client via SignalR.

like image 85
Oleksandr Kobylianskyi Avatar answered Oct 13 '22 10:10

Oleksandr Kobylianskyi