MVC 中的动态下拉列表
Dynamic DropDown list in MVC
我是 MVC 的新手,我正在尝试在 MVC 中创建一个 CreateEmployee 表单。现在,我想要实现的只是将部门的填充下拉列表添加到表单中。下拉列表是从数据库中填充的,使用 Visual Studio,我连接到数据库并为 table 创建了所有代码文件。这就是创建表单的样子。下面的表单是使用 Angular js.
创建的
这是我的 Createform 模型。
public class CreateEmployee
{
public int Id { get; set; }
public string FullName { get; set; }
[Required]
public string Notes { get; set; }
//add the dropdown list of departments here,i not sure on what to do here
//do i create an instance of dbcontext here or AngtestDepartment
public bool PerkCar { get; set; }
public bool PerkStock { get; set; }
public bool PerkSixWeeks { get; set; }
public string PayrollType { get; set; }
}
public ActionResult CreateEmployee()
{
return View();
}
这是 visual studio 生成的 table 代码
部门Table
using System;
using System.Collections.Generic;
public partial class AngTestDepartment
{
public int id { get; set; }
public string Department { get; set; }
}
和部门 Table 上下文
using System;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
public partial class DepartmentDbContext : DbContext
{
public DepartmentDbContext()
: base("name=DepartmentDbContext")
{
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
throw new UnintentionalCodeFirstException();
}
public virtual DbSet<AngTestDepartment> AngTestDepartments { get; set; }
public System.Data.Entity.DbSet<AngularForms2.Models.CreateEmployee> CreateEmployees { get; set; }
}
您应该在控制器中查询数据库并将其提供给视图,例如通过模型:
将以下内容添加到您的模型中:
public int Department { get; set; }
public IEnumerable<AngTestDepartment> Departments { get; set; }
在你的操作中:
public ActionResult CreateEmployee()
{
using (var db = new DepartmentDbContext())
{
var model = new CreateEmployee();
model.Departments = db.AngTestDepartments.ToList();
return View(model);
}
}
然后在您的视图中,您可以执行以下操作:
@Html.DropDownListFor(m => m.Department,
Model.Departments.Select(d => new SelectListItem()
{
Value = d.id.ToString(),
Text = d.Department
}))
我是 MVC 的新手,我正在尝试在 MVC 中创建一个 CreateEmployee 表单。现在,我想要实现的只是将部门的填充下拉列表添加到表单中。下拉列表是从数据库中填充的,使用 Visual Studio,我连接到数据库并为 table 创建了所有代码文件。这就是创建表单的样子。下面的表单是使用 Angular js.
创建的这是我的 Createform 模型。
public class CreateEmployee
{
public int Id { get; set; }
public string FullName { get; set; }
[Required]
public string Notes { get; set; }
//add the dropdown list of departments here,i not sure on what to do here
//do i create an instance of dbcontext here or AngtestDepartment
public bool PerkCar { get; set; }
public bool PerkStock { get; set; }
public bool PerkSixWeeks { get; set; }
public string PayrollType { get; set; }
}
public ActionResult CreateEmployee()
{
return View();
}
这是 visual studio 生成的 table 代码
部门Table
using System;
using System.Collections.Generic;
public partial class AngTestDepartment
{
public int id { get; set; }
public string Department { get; set; }
}
和部门 Table 上下文
using System;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
public partial class DepartmentDbContext : DbContext
{
public DepartmentDbContext()
: base("name=DepartmentDbContext")
{
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
throw new UnintentionalCodeFirstException();
}
public virtual DbSet<AngTestDepartment> AngTestDepartments { get; set; }
public System.Data.Entity.DbSet<AngularForms2.Models.CreateEmployee> CreateEmployees { get; set; }
}
您应该在控制器中查询数据库并将其提供给视图,例如通过模型:
将以下内容添加到您的模型中:
public int Department { get; set; }
public IEnumerable<AngTestDepartment> Departments { get; set; }
在你的操作中:
public ActionResult CreateEmployee()
{
using (var db = new DepartmentDbContext())
{
var model = new CreateEmployee();
model.Departments = db.AngTestDepartments.ToList();
return View(model);
}
}
然后在您的视图中,您可以执行以下操作:
@Html.DropDownListFor(m => m.Department,
Model.Departments.Select(d => new SelectListItem()
{
Value = d.id.ToString(),
Text = d.Department
}))