使用 Entity Framework 6 和 ViewModel 从数据库 table 在 ASP.NET MVC 中填充 DropDownList
Populate DropDownList in ASP.NET MVC from database table using Entity Framework 6 and ViewModel
为了一个可以使用 ajax/jquery 和存储过程快速解决的问题,我绞尽脑汁整整一夜。我要
1) 使用 Entity Framework 和视图模型从数据库 table 获得的值填充下拉列表。我不想使用 VIEWBAG 或 VIEWDATA。任何帮助表示赞赏。
2) 如何使用具有所有默认字段的视图模型生成创建视图?脚手架适用于模型但不适用于视图模型?
我的模型
public class Employee
{
public int EmployeeID { get; set; }
public string Name { get; set; }
public string Gender { get; set; }
public string City { get; set; }
public string Level { get; set; }
public int DepartmentId { get; set; }
}
public class Grade
{
public int ID { get; set; }
public string Level { get; set; }
}
查看模型
public class GradeSelectListViewModel
{
public Employee Employee { get; set; }
public IEnumerable<SelectListItem> Grades { get; set; }
public GradeSelectListViewModel(Employee employee, IEnumerable grades)
{
Employee = employee;
Grades = new SelectList(grades, "Grade", "Name", employee.Level);
}
}
我的背景CLASS
public class EmployeeContext : DbContext
{
public DbSet<Employee> Employees { get; set; }
public DbSet<Department> Departments { get; set; }
public DbSet<Grade> Grades { get; set; }
}
我的控制器
public ActionResult Edit (int? id)
{
using (var db = new EmployeeContext())
{
var model = new GradeSelectListViewModel(db.Employees.Find(id), db.Grades);
//model.Employee = db.Employees.Single(x => x.EmployeeID == id);
model.Grades = db.Grades.ToList().Select(x => new SelectListItem
{
Value = x.ID.ToString(),
Text = x.Level
});
return View(model);
}
}
我的 RAZOR 页面 CSHTML
@model MVCDemo.ViewModels.GradeSelectListViewModel
....
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
....
@Html.DropDownListFor(x => Model.Employee.Level,
new SelectList(Model.Grades, "ID", "Level"),
"Select Level")
....
<input type="submit" value="Create" class="btn btn-default" />
}
主要问题是在视图中您有 new SelectList(Model.Grades, "ID", "Level")
但 Grades
是 IEnumerable<SelectListItem>
并且 SelectListItem
不包含名为 ID
和 Level
.
但是您的代码还有一些其他问题。首先,视图模型不应包含数据模型,而您的视图模型应该是
public class GradeSelectListViewModel
{
public int? ID { get; set; } // make this ID so you do not need an input for it
public string Name { get; set; }
.... // other properties of Employee that your editing
[Required(ErrorMessage = "..")]
public int? Level { get; set; } // make value types nullable to protect against under-posting attacks
public IEnumerable<SelectListItem> Grades { get; set; }
}
并根据需要添加显示和验证属性。注意我删掉了构造函数(你好像没有用,但是如果你用了,那么你还需要包含一个无参数的构造函数,否则提交到POST方法时会抛出异常. 我还假设 Level
应该是 typeof int
因为你绑定到 Grade
.
的 int ID
属性
GET 方法中的代码应该是
Employee employee = db.Employees.Find(id);
var model = new GradeSelectListViewModel()
{
ID = employee.EmployeeID,
Name = employee.Name,
Level = employee.Level, // convert to int?
....
Grades = db.Grades.Select(x => new SelectListItem
{
Value = x.ID.ToString(),
Text = x.Level
})
};
return View(model);
并在视图中
@Html.DropDownListFor(x => Model.Level, Model.Grades, "Select Level")
另请注意,在 POST 方法中,如果 return 视图因为 ModelState
无效,则需要重新分配 SelectList
。
您可以使用以下方法在同一视图中填充三个 DropDownListFor:
ViewModel:
public class GroupViewModel
{
public IEnumerable<SelectListItem> Schedules { get; set; }
public int ScheduleId { get; set; }
public IEnumerable<SelectListItem> Labs { get; set; }
public int LabId { get; set; }
public IEnumerable<SelectListItem> Terms { get; set; }
public int TermId { get; set; }
}
控制器:
public ActionResult Create()
{
//Populate DropDownList binding values
var model = new GroupViewModel
{
//Preselect the Lab with id 2
//LabId = 2,
Labs = repository.Labs.Select(c => new SelectListItem
{
Value = c.Id.ToString(),
Text = c.Name
}),
Terms = repository.Terms.Select(c => new SelectListItem
{
Value = c.Id.ToString(),
Text = c.Name
}),
Schedules = repository.Schedules.Select(c => new SelectListItem
{
Value = c.Id.ToString(),
Text = c.Name
})
};
return View("Create", model);
}
查看:
@Html.DropDownListFor(m => m.LabId, new SelectList(Model.Labs, "Value", "Text"),
"Select", new { @class = "selectpicker" })
@Html.DropDownListFor(m => m.ScheduleId, new SelectList(Model.Schedules, "Value", "Text"),
"Select", new { @class = "selectpicker" })
@Html.DropDownListFor(m => m.TermId, new SelectList(Model.Terms, "Value", "Text"),
"Select", new { @class = "selectpicker" })
希望这对您有所帮助...
为了一个可以使用 ajax/jquery 和存储过程快速解决的问题,我绞尽脑汁整整一夜。我要
1) 使用 Entity Framework 和视图模型从数据库 table 获得的值填充下拉列表。我不想使用 VIEWBAG 或 VIEWDATA。任何帮助表示赞赏。
2) 如何使用具有所有默认字段的视图模型生成创建视图?脚手架适用于模型但不适用于视图模型?
我的模型
public class Employee
{
public int EmployeeID { get; set; }
public string Name { get; set; }
public string Gender { get; set; }
public string City { get; set; }
public string Level { get; set; }
public int DepartmentId { get; set; }
}
public class Grade
{
public int ID { get; set; }
public string Level { get; set; }
}
查看模型
public class GradeSelectListViewModel
{
public Employee Employee { get; set; }
public IEnumerable<SelectListItem> Grades { get; set; }
public GradeSelectListViewModel(Employee employee, IEnumerable grades)
{
Employee = employee;
Grades = new SelectList(grades, "Grade", "Name", employee.Level);
}
}
我的背景CLASS
public class EmployeeContext : DbContext
{
public DbSet<Employee> Employees { get; set; }
public DbSet<Department> Departments { get; set; }
public DbSet<Grade> Grades { get; set; }
}
我的控制器
public ActionResult Edit (int? id)
{
using (var db = new EmployeeContext())
{
var model = new GradeSelectListViewModel(db.Employees.Find(id), db.Grades);
//model.Employee = db.Employees.Single(x => x.EmployeeID == id);
model.Grades = db.Grades.ToList().Select(x => new SelectListItem
{
Value = x.ID.ToString(),
Text = x.Level
});
return View(model);
}
}
我的 RAZOR 页面 CSHTML
@model MVCDemo.ViewModels.GradeSelectListViewModel
....
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
....
@Html.DropDownListFor(x => Model.Employee.Level,
new SelectList(Model.Grades, "ID", "Level"),
"Select Level")
....
<input type="submit" value="Create" class="btn btn-default" />
}
主要问题是在视图中您有 new SelectList(Model.Grades, "ID", "Level")
但 Grades
是 IEnumerable<SelectListItem>
并且 SelectListItem
不包含名为 ID
和 Level
.
但是您的代码还有一些其他问题。首先,视图模型不应包含数据模型,而您的视图模型应该是
public class GradeSelectListViewModel
{
public int? ID { get; set; } // make this ID so you do not need an input for it
public string Name { get; set; }
.... // other properties of Employee that your editing
[Required(ErrorMessage = "..")]
public int? Level { get; set; } // make value types nullable to protect against under-posting attacks
public IEnumerable<SelectListItem> Grades { get; set; }
}
并根据需要添加显示和验证属性。注意我删掉了构造函数(你好像没有用,但是如果你用了,那么你还需要包含一个无参数的构造函数,否则提交到POST方法时会抛出异常. 我还假设 Level
应该是 typeof int
因为你绑定到 Grade
.
int ID
属性
GET 方法中的代码应该是
Employee employee = db.Employees.Find(id);
var model = new GradeSelectListViewModel()
{
ID = employee.EmployeeID,
Name = employee.Name,
Level = employee.Level, // convert to int?
....
Grades = db.Grades.Select(x => new SelectListItem
{
Value = x.ID.ToString(),
Text = x.Level
})
};
return View(model);
并在视图中
@Html.DropDownListFor(x => Model.Level, Model.Grades, "Select Level")
另请注意,在 POST 方法中,如果 return 视图因为 ModelState
无效,则需要重新分配 SelectList
。
您可以使用以下方法在同一视图中填充三个 DropDownListFor:
ViewModel:
public class GroupViewModel
{
public IEnumerable<SelectListItem> Schedules { get; set; }
public int ScheduleId { get; set; }
public IEnumerable<SelectListItem> Labs { get; set; }
public int LabId { get; set; }
public IEnumerable<SelectListItem> Terms { get; set; }
public int TermId { get; set; }
}
控制器:
public ActionResult Create()
{
//Populate DropDownList binding values
var model = new GroupViewModel
{
//Preselect the Lab with id 2
//LabId = 2,
Labs = repository.Labs.Select(c => new SelectListItem
{
Value = c.Id.ToString(),
Text = c.Name
}),
Terms = repository.Terms.Select(c => new SelectListItem
{
Value = c.Id.ToString(),
Text = c.Name
}),
Schedules = repository.Schedules.Select(c => new SelectListItem
{
Value = c.Id.ToString(),
Text = c.Name
})
};
return View("Create", model);
}
查看:
@Html.DropDownListFor(m => m.LabId, new SelectList(Model.Labs, "Value", "Text"),
"Select", new { @class = "selectpicker" })
@Html.DropDownListFor(m => m.ScheduleId, new SelectList(Model.Schedules, "Value", "Text"),
"Select", new { @class = "selectpicker" })
@Html.DropDownListFor(m => m.TermId, new SelectList(Model.Terms, "Value", "Text"),
"Select", new { @class = "selectpicker" })
希望这对您有所帮助...