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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
käµfm³d 👽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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.