Having an issue where return View(await _context.Reviews.ToListAsync);
gives the following error: Cannot await 'method group'
I'm lead to believe that to use the return View(await _context.Reviews.ToListAsync);
statement, I need to use using ASPNETCoreWebApplication.data
(hence my inclusion of it), but it returns an error: The type or namespace 'ASPNETCoreWebApplication' could not be found [...]
.
If I remove using <project_name>data
, there is the same error as above for ApplicationDbContext
in HomeController.cs
)
Below is my 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();
}
}
}
In case it may be relevant, here is my Reviews.cs
Model:
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; }
}
}
In HomeController.cs
,
Don't make the rookie error of forgetting parentheses:
public async Task<IActionResult> Reviews()
{
return View(await _context.Reviews.ToListAsync());
}