我不知道我做错了什么,它只是一个常规的asp.net mvc5项目。这是我的控制器:
private ApplicationDbContext db = new ApplicationDbContext();
// GET: Items
public ActionResult Index(string sortOrder, string currentFilter, string searchString, int? page)
{
ViewBag.CurrentSort = sortOrder;
ViewBag.NameSortParm = String.IsNullOrEmpty(sortOrder) ? "name_desc" : "";
ViewBag.PriceSortParm = sortOrder == "Price" ? "price_desc" : "Price";
if (searchString != null)
{
page = 1;
}
else
{
searchString = currentFilter;
}
ViewBag.CurrentFilter = searchString;
var items = from i in db.Items
select i;
if (!String.IsNullOrEmpty(searchString))
{
items = items.Where(s => s.Name.ToUpper().Contains(searchString.ToUpper())
|| s.Catagorie.Name.ToUpper().Contains(searchString.ToUpper()));
}
switch (sortOrder)
{
case "name_desc":
items = items.OrderByDescending(s => s.Name);
break;
case "Price":
items = items.OrderBy(s => s.Price);
break;
case "price_desc":
items = items.OrderByDescending(s => s.Price);
break;
default: // Name ascending
items = items.OrderBy(s => s.Name);
break;
}
int pageSize = 3;
int pageNumber = (page ?? 1);
return System.Web.UI.WebControls.View( items.ToPagedList(pageNumber, pageSize));
//var items = db.Items.Include(i => i.Catagorie);
//return View(await items.ToListAsync());
}
// GET: Items/Details/5
public async Task<ActionResult> Details(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
Item item = await db.Items.FindAsync(id);
if (item == null)
{
return HttpNotFound();
}
return View(item);
}
// GET: Items/Create
[Authorize(Roles = "Admin")]
public ActionResult Create()
{
ViewBag.CatagorieId = new SelectList(db.Catagories, "ID", "Name");
return View();
}
// POST: Items/Create
[HttpPost]
[ValidateAntiForgeryToken]
[Authorize(Roles = "Admin")]
public async Task<ActionResult> Create(Item item)
{
if (ModelState.IsValid)
{
db.Items.Add(item);
await db.SaveChangesAsync();
return RedirectToAction("Index");
}
ViewBag.CatagorieId = new SelectList(db.Catagories, "ID", "Name", item.CatagorieId);
return View(item);
}
// GET: Items/Edit/5
[Authorize(Roles = "Admin")]
public async Task<ActionResult> Edit(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
Item item = await db.Items.FindAsync(id);
if (item == null)
{
return HttpNotFound();
}
ViewBag.CatagorieId = new SelectList(db.Catagories, "ID", "Name", item.CatagorieId);
return View(item);
}
// POST: Items/Edit/5
[HttpPost]
[ValidateAntiForgeryToken]
[Authorize(Roles = "Admin")]
public async Task<ActionResult> Edit(Item item)
{
if (ModelState.IsValid)
{
db.Entry(item).State = EntityState.Modified;
await db.SaveChangesAsync();
return RedirectToAction("Index");
}
ViewBag.CatagorieId = new SelectList(db.Catagories, "ID", "Name", item.CatagorieId);
return View(item);
}
// GET: Items/Delete/5
[Authorize(Roles = "Admin")]
public async Task<ActionResult> Delete(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
Item item = await db.Items.FindAsync(id);
if (item == null)
{
return HttpNotFound();
}
return View(item);
}
// POST: Items/Delete/5
[HttpPost, ActionName("Delete")]
[ValidateAntiForgeryToken]
[Authorize(Roles = "Admin")]
public async Task<ActionResult> DeleteConfirmed(int id)
{
Item item = await db.Items.FindAsync(id);
db.Items.Remove(item);
await db.SaveChangesAsync();
return RedirectToAction("Index");
}
public async Task<ActionResult> RenderImage(int id)
{
Item item = await db.Items.FindAsync(id);
byte[] photoBack = item.InternalImage;
return File(photoBack, "image/png");
}
protected override void Dispose(bool disposing)
{
if (disposing)
{
db.Dispose();
}
base.Dispose(disposing);
}
我收到以下错误Severity Code Description Project File Line
Error CS1061 'ApplicationDbContext' does not contain a definition for 'Items' and no extension method 'Items' accepting a first argument of type 'ApplicationDbContext' could be found (are you missing a using directive or an assembly reference?)
但在我的数据库中,我确实有Items表,而且我指的是正确的数据库。
答案 0 :(得分:1)
你应该有这样的东西:
public class ApplicationDbContext : DbContext
{
public DbSet<Item> Items { get; set; }
// Rest of data models
}
如果没有,那就不行了。