Avatar of sbornstein2
sbornstein2
 asked on

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?
ASP.NET

Avatar of undefined
Last Comment
kaufmed

8/22/2022 - Mon
kaufmed

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.
sbornstein2

ASKER
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.
ASKER CERTIFIED SOLUTION
kaufmed

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes