0

遇到问题return View(await _context.Reviews.ToListAsync);时会出现以下错误:Cannot await 'method group'

我被引导相信要使用该return View(await _context.Reviews.ToListAsync);语句,我需要使用using ASPNETCoreWebApplication.data(因此我包含它),但它返回一个错误:The type or namespace 'ASPNETCoreWebApplication' could not be found [...].

如果我删除using <project_name>data,则会出现与上述相同的ApplicationDbContext错误HomeController.cs

下面是我的HomeController.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using ASPNETCoreWebApplication.Data; // "The type or namespace 'ASPNETCoreWebApplication' could not be found [...]"
using <project_name>.Data;
using Microsoft.EntityFrameworkCore;

namespace <project_name>.Controllers
{
    public class HomeController : Controller
    {
        private readonly ApplicationDbContext _context;

        public HomeController(ApplicationDbContext context)
        {
            _context = context;
        }

        public async Task<IActionResult> Reviews()
        {
            // "Cannot await 'method group'"
            return View(await _context.Reviews.ToListAsync);
        }

        public IActionResult Error()
        {
            return View();
        }
    }
}

如果它可能相关,这是我的Reviews.cs模型:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace <project_name>.Models
{
    public class Reviews
    {
        public int Id { get; set; }
        public string Date { get; set; }
        public string Name { get; set; }
        public string Heading { get; set; }
        public string Restaurant { get; set; }
        public string Comment { get; set; }
        public int Rating { get; set; }
        public int Agree { get; set; }
        public int Disagree { get; set; }
    }
}

HomeController.cs,

4

1 回答 1

3

不要犯忘记括号的菜鸟错误:

public async Task<IActionResult> Reviews()
        {
            return View(await _context.Reviews.ToListAsync());
        }
于 2017-11-04T10:33:40.340 回答