ASP.Net MVC IActionResult question

How do I make this an async action result if I am using auto mapper.

        public async Task<IActionResult> Index()
        {
            var items = _context.Customers.ToListAsync();
            var mappedItems = _mapper.Map<List<CustomerViewModel>>(items);

            return View(mappedItems);
        }

The issue is from the context I can use ToListAsync but I can't use the await such as return View(await mappedItems);.   What is the best way to handle this when using automapper?
sbornstein2Asked:
Who is Participating?
 
käµfm³d 👽Connect With a Mentor Commented:
I might not be understanding your comment, but marking a method as async simply means that you can use the await keyword in the method body. If you're using (async)await, then your method is asynchronous. Using async/await means that your method has to return Task<Something>--there's no way around that.

So if your code was complaining of an error or not working, then you'd need to show the offending code as-is, and describe the error/behavior.
0
 
käµfm³d 👽Commented:
await the ToListAsync call:

var items = await _context.Customers.ToListAsync();

Open in new window


Basically, await a task wherever you absolutely cannot continue further without the value that the task holds. It doesn't necessarily need to happen on the same line as the async call itself, but in your case it does need to because your mapping code is dependent on that value.
0
 
sbornstein2Author Commented:
But if I do that then the action result needs to not be async at that point so that was my concern.  I tried that and that did work on the list but the action result then needed to be a regular sync which did not make sense to me.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.