ViewComponent对我的查询不满意

时间:2018-01-21 12:06:47

标签: c# asp.net-core asp.net-core-viewcomponent

我很想掌握ViewComponents。我有一个购物车的ViewComponent类:

public class ShoppingCartViewComponent : ViewComponent
{
    private readonly MyStoreContext _context;

    public ShoppingCartViewComponent(MyStoreContext context)
    {
        _context = context;
    }

    public async Task<IViewComponentResult> InvokeAsync(int Id)
    {
        return View(await GetCartAsync(Id));
    }

    private Task<ViewModelShoppingCart> GetCartAsync(int Id)
    {
        var VMCart = _context.ShoppingCarts
                    .Where(c => c.Id == Id)
                    .Select(cart => new ViewModelShoppingCart
                    {
                        Id = cart.Id,
                        Title = cart.Title,
                        CreateDate = cart.CreateDate,
                        ShoppingCartItems = cart.ShoppingCartItems
                                            .Select(items => new ViewModelShoppingCartItem
                                            {
                                                ProductId = items.ProductId,
                                                ProductTitle = items.Product.Title,
                                                ProductPrice = items.Product.Price,
                                                Quantity = items.Quantity
                                            }).ToList()
                    }).FirstOrDefault();
        return VMCart;
    }
}

这导致return VMCart上的构建错误,来自VS的消息:

Cannot implicitly convert type 'System.Linq.IQueryable<MyStore.Models.ViewModels.ViewModelShoppingCart>' to 'System.Threading.Tasks.Task<MyStore.Models.ViewModels.ViewModelShoppingCart>'. An explicit conversion exists (are you missing a cast?)

我做错了什么? 编辑在查询结束时添加了.FirstOrDefault(),但错误仍然相同。

1 个答案:

答案 0 :(得分:2)

问题是您的函数定义看起来像是在尝试创建异步方法,但内部没有任何异步。将其更改为:

private async Task<ViewModelShoppingCart> GetCartAsync(int Id)
{
    var VMCart = await _context.ShoppingCarts
                .Where(c => c.Id == Id)
                .Select(cart => new ViewModelShoppingCart
                {
                    Id = cart.Id,
                    Title = cart.Title,
                    CreateDate = cart.CreateDate,
                    ShoppingCartItems = cart.ShoppingCartItems
                                        .Select(items => new ViewModelShoppingCartItem
                                        {
                                            ProductId = items.ProductId,
                                            ProductTitle = items.Product.Title,
                                            ProductPrice = items.Product.Price,
                                            Quantity = items.Quantity
                                        }).ToList()
                }).FirstOrDefaultAsync();
    return VMCart;
}

或者让它不是异步的:

private ViewModelShoppingCart GetCart(int Id)
{
    var VMCart = _context.ShoppingCarts
                .Where(c => c.Id == Id)
                .Select(cart => new ViewModelShoppingCart
                {
                    Id = cart.Id,
                    Title = cart.Title,
                    CreateDate = cart.CreateDate,
                    ShoppingCartItems = cart.ShoppingCartItems
                                        .Select(items => new ViewModelShoppingCartItem
                                        {
                                            ProductId = items.ProductId,
                                            ProductTitle = items.Product.Title,
                                            ProductPrice = items.Product.Price,
                                            Quantity = items.Quantity
                                        }).ToList()
                }).FirstOrDefault();
    return VMCart;
}