我假设我错过了在控制器顶部声明的命名空间,但我尝试使用'using WebMatrix.Data;'和'使用System.IO;'他们是这个错误的通常建议。它没用。
我正在尝试在同一视图中显示多个数据库表,而这个错误阻止了我。请帮助。
我的控制器:
using System.Linq;
using System.Web.Mvc;
using KMS.Models;
using WebMatrix.Data;
using System.IO;
namespace KMS.Controllers
{
public class KMSController : Controller{
public ActionResult Index()
{
KMSConection cs = new KMSConection();
cs.Areas = (from o in db.Areas select o).Tolist();
cs.AreaTypes = (from o in db.AreaTypes select or).Tolist();
return View(cs);
}
}
}
我的ViewModel类:
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Web;
using KMS.Models;
namespace KMS.Models
{
public class KMSConection: ApplicationDbContext
{
public DbSet<Area> Areas { get; set; }
public DbSet<AreaType> AreaTypes { get; set; }
}
}
谢谢!
答案 0 :(得分:0)
因为您没有提到您在KMSController中创建的db变量。尝试使用此代码来修复错误:
using System.Linq;
using System.Web.Mvc;
using KMS.Models;
using WebMatrix.Data;
using System.IO;
namespace KMS.Controllers
{
public class KMSController : Controller{
public ActionResult Index()
{
//Insert the actual entity referred to your db variable.
//Probably something like:
//var db = Some Database Context
KMSConection cs = new KMSConection();
cs.Areas = (from o in db.Areas select o).Tolist();
cs.AreaTypes = (from o in db.AreaTypes select or).Tolist();
return View(cs);
}
}
}