0

I have function produced Model asynchronously, function working fine.

Async Function ReadLocations(Skip As Integer) As Task(Of Model.ViewLocation)
    ...
    Return Await Task.Run(Function() Model)
End Function

I creating an async controller using this Model and may be confused with the right syntax to load the Async model to View. Where is the right place for Await? And what right way to create a reference to async delegate who will create a model - Task.FromResult or Task.Result?

Public Async Function Index(id as integer) As Task(Of ActionResult)
    ...
    If UserState IsNot Nothing Then
         Dim Model As Model.ViewLocation = Await Task.Run(Function() ReadLocations(id))
         Dim ModelTask As Task(Of Model.ViewLocation) = ReadLocations(id)
         '--------
         Return ???  View("Index", Await Model/ModelTask)  ???
         or 
         Return Await ??? Task.Run(Function() View("Index", Model/ModelTask)) ???
         '--------
    Else
        Return Await Task.Run(Function() RedirectToAction("Index", "Home"))
    End If
End Function
5
  • Is the ReadLocations method actually performing asynchronous workloads other than the Task.Run you have in there? Are you using that Task.Run as a simple example of asynchronous work building and returning your model? Commented Apr 1, 2021 at 0:23
  • You do not need to wrap your View or RedirectToAction inside of a Task. The only time you need to Await is when you call your method to obtain your model. It seems to be the only asynchronous work that you are doing in that Index method Commented Apr 1, 2021 at 0:34
  • If ReadLocations isn't actually performing any asynchronous work to obtain your model, then you can get rid of all the Async/Await stuff completely Commented Apr 1, 2021 at 0:40
  • @RexHenderson ReadLocations - is complex and sophisticated function with a lot of request to backend. Commented Apr 1, 2021 at 8:35
  • @RexHenderson Model in my case impossible to directly pass to function View, because error is appear ""Since this is an async method, the return expression must be of type 'ActionResult' rather than 'Task(Of ActionResult)'" Commented Apr 1, 2021 at 8:43

2 Answers 2

1

This is an example of all you need to do.

Imports System.Threading.Tasks

Public Class FooModel
    Public Message As String
End Class

Public Class HomeController
    Inherits System.Web.Mvc.Controller

    Async Function Index(id As Int32) As Task(Of ActionResult)

        Dim model = Await ReturnLocationsAsync(id)

        Return View(model)

    End Function

    Async Function ReturnLocationsAsync(Skip As Int32) As Task(Of FooModel)

        'pretend were calling out to some async process that gets our model
        Return Await Task.Run(Function() New FooModel With {.Message = "Hello"})

    End Function

End Class
Sign up to request clarification or add additional context in comments.

1 Comment

This is wrong syntax. If I type 'Dim M = Await ReadLocations(0) Return View("Index", M)' I receive "Since this is an async method, the return expression must be of type 'ActionResult' rather than 'Task(Of ActionResult)'"
0

ha, I found an issue. I have my own function VIEW, and this function redefined embedded MS function Controller.View. So, right syntax is

Return Await Task.Run(Function() View("Index", ReadLocations(id).Result)) 

2 Comments

It's not really making sense why you feel you need to return a View like this. Maybe if you post your code more clearly.
@RexHenderson This function I use in pager

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.