-2

I am trying to create a async action in mvc 4.

 public async Task<ActionResult> Index(bool MyList, int Verified, string searchText)
        {
 ProductViewModel pvm =await new ProductViewModel(MyList, Verified, searchText);
            return View(pvm);
}

In the above code where I am calling ProductViewModel constructor it shows

cannot await ProductViewModel

After this I tried to make my constructor async but the constructor shows error,

This async method lacks 'await' operator

I have created the ProductViewModel as follows,

 public class ProductListViewModel
    {
        public MyEntities _context;
        public bool _verified; 

       public async Task<ProductViewModel> ProductViewModel(bool MyList, int Verified, string searchText)
        {
            _context = new MyEntities();
            this.search = searchText;
            this.SelectedListType = MyList;
            this._verified= Verified;

        }

    public int SelectedListType { get; set; }
        public IEnumerable<SelectListItem> VerifiedTypeList
        {
            get
            {
                List<ListTypeViewModel> ltvm = new List<ListTypeViewModel>();
                ltvm.Add(new ListTypeViewModel(1,"Verified"));
                ltvm.Add(new ListTypeViewModel(0,"Pending"));
                var allVerifiedType = ltvm.Select(c => new SelectListItem
                {
                    Value = c.ListValue.ToString(),
                    Text = c.ListText

                });
                return DefaultVerifiedTypeList.Concat(allVerifiedType);
            }
        }
        public IEnumerable<SelectListItem> DefaultVerifiedTypeList
        {
            get
            {
                return Enumerable.Repeat(new SelectListItem
                {
                    Value = "-1",
                    Text = "All"
                }, count: 1);
            }
        }
  public List<ProductViewmodel> ProductList
        {
            get
            {
                List<ProductViewmodel> pdvmList = new List<ProductViewmodel>();
                foreach (var pm in _context.GetProductList(SelectedListType, _verified, search).ToList())
                {
                    try
                    {
                        ProductViewmodel pdvm = new ProductViewmodel();
                        pdvm.Price = pm.Price;
                        pdvm.ProductID = pm.ProductID;
                        pdvm.ProductName = System.Threading.Thread.CurrentThread.CurrentCulture.TextInfo.ToTitleCase(pm.ProductName.ToLower());
                        pdvm.IsVerified = pm.Verified;
                        pdvmList.Add(pdvm);
                    }
                    catch (Exception error)
                    {
                        Console.WriteLine(error.Message);
                    }
                }
                return pdvmList;
            }
        }
}

Thanks in advance.

Abhishek Pandey
  • 338
  • 1
  • 3
  • 13
  • 1 - Why you need that action to be async? Is there any thread blocking operations done in that action? 2- May be personal opinion, but that view model has too much responsibilities and if I'm not wrong it is using EF context and hitting database as well - seems like a pretty mess. – Developer Feb 10 '17 at 06:04
  • yes you are right, actually my action is taking too long to load the web page. – Abhishek Pandey Feb 10 '17 at 06:08
  • MVC or not, you can't make constructors async. See marked duplicate for details. – Peter Duniho Feb 10 '17 at 06:48

1 Answers1

-1

Not sure constructors can be asynchronous, but you can call them asynchronously by instantiating the object asynchronously:

ProductViewModel pvm = null;
await Task.Run(async () => {
                               pvm = new ProductViewModel(MyList, Verified, searchText);
                           });
John Wu
  • 50,556
  • 8
  • 44
  • 80