如何在mvc 4中异步调用ViewModel

时间:2017-02-10 05:56:22

标签: c# asp.net-mvc

我正在尝试在mvc 4中创建异步操作。

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

在我调用ProductViewModel构造函数的上面的代码中,它显示了

  

无法等待ProductViewModel

在此之后,我尝试使我的构造函数异步,但构造函数显示错误,

  

这种异步方法缺少'await'运算符

我创建了ProductViewModel,如下所示,

 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;
            }
        }
}

提前致谢。

1 个答案:

答案 0 :(得分:-1)

不确定构造函数是否可以异步,但您可以通过异步实例化对象来异步调用它们:

ProductViewModel pvm = null;
await Task.Run(async () => {
                               pvm = new ProductViewModel(MyList, Verified, searchText);
                           });