Reputation: 35
I have a database CarsDB
with a table Car
with some columns (like mileage or model)
And I want to get info from that table in a controller I write:
public ActionResult Index()
{
return View(db.CarsDB.ToList()):
}
But I'm getting an error (something like method CarsDB not found). The error is long and I don't know hot to translate it to English :( Also, when I write db, the only suggetions I'm getting are "Equals", "GetHashCode" and some others, not with CarsDB
Upvotes: 0
Views: 64
Reputation: 7141
If you're using Entity Framework you could do something like:
public ActionResult Index()
{
using (CarsDB dataContext = new CarsDB())
{
return View(dataContext.Cars.ToList()):
}
}
This will also assure automatic disposal of your data context.
Upvotes: 1
Reputation: 15140
Try
db.Cars.ToList()
You need to query the table (cars) - right now you're saying "select everything from the CarsDB database", rather than "select everything from the Cars table in the CarsDB database".
To clarify - your db
variable should be an instance of a data context or entities.
Full code should look something like this:
public ActionResult Index()
{
CarsDB db = new CarsDB();
return View(db.Cars.ToList()):
}
Upvotes: 2