遇到问题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
,