我正在尝试为部门名称制作下拉列表。我正在使用MVC5。我看到了太多关于堆栈溢出的解决方案,但我从未找到与 MVC5 相关的有价值的解决方案。
Database Name : AppraisalDBContext
Table Name : Department
Column Name : deptID (Primarykey)
deptName (department name)
Description
我收到此错误:
An exception of type 'System.InvalidOperationException' occurred in EntityFramework.dll but was not handled in user code
Additional information: The model backing the 'AppraisalDBContext' context has changed since the database was created.Consider using Code First Migrations to update the database (http://go.microsoft.com/fwlink/?
Code:
控制器类名称(DepartmentController.cs):
public ActionResult Index(string depName)
{
var DeptLst = new List<string>();
var GenreQry = from d in db.Department
orderby d.deptName
select d.deptName;
DeptLst.AddRange(GenreQry); // Here I am confusing
ViewBag.depName = new SelectList(DeptLst); // Here I am confusing
var depts = from m in db.Department // Here I am confusing
select m;
return View(depts);
}
模型类名称(Departments.cs):
namespace appraisalProject.Models
{
[Table("Department")]
public class Departments
{
[Key]
public int deptId { get; set; }
public string deptName { get; set; }
public string Description { get; set; }
}
}
模型类名称(AppraisalDBContext.cs):
namespace appraisalProject.Models
{
public class AppraisalDBContext:DbContext
{
public DbSet<Departments> Department { get; set; }
}
}
索引.cshtml:
@using (Html.BeginForm())
{
<p>
Genre: @Html.DropDownList("depts", "All")
<input type="submit" value="Filter" />
</p>
}