问题描述
我整夜都在努力解决一个问题,我可以使用ajax/jquery和存储过程来快速完成此问题.我想
I have been scratching my head for a whole night on an issue I can do quickly using ajax/jquery and stored procedures. I want to
1)使用实体框架和视图模型,从从数据库表获得的值填充下拉列表.我不想使用VIEWBAG或VIEWDATA.任何帮助表示赞赏.
1) Populate a drop down list from values obtained from a database table using Entity Framework and view model. I DO NOT WANT TO USE VIEWBAG OR VIEWDATA. Any help appreciated.
2)如何使用带有所有默认字段的视图模型生成创建视图?脚手架只能在模型上工作,而不能在视图模型上工作?
2) How can I generate a Create View using the View Model with the all the default fields ? The scaffholding works on a model but not on a view model ?
我的模型
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);
}
}
我的语境课
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 PAGE CSHTML
MY RAZOR PAGE 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
.
The main issue is that in the view you have new SelectList(Model.Grades, "ID", "Level")
but Grades
is IEnumerable<SelectListItem>
and SelectListItem
does not contain properties named ID
and Level
.
但是您的代码还有其他一些问题.首先,视图模型不应包含数据模型,而您的视图模型应为
However there are a a few other issues with your code. First a view model should not contain a data model, and instead your view model should be
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方法时将引发异常.我也假设由于您绑定到Grade
的int ID
属性,因此Level
应该为type int
.
and add display and validation attributes as required. Note that I deleted the constructor (you don't seem to be using it, but if you did, then you also need to include a parameter-less constructor, otherwise an exception will be thrown when submitting to the POST method. I also assume that Level
should be typeof int
since you binding to the int ID
property of Grade
.
GET方法中的代码应为
The the code in your GET method should be
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方法中,由于ModelState
无效,因此如果您返回视图,则需要重新分配SelectList
.
Note also that in the POST method, your need to reassign the SelectList
if you return the view because ModelState
is invalid.
这篇关于使用Entity Framework 6和ViewModel从数据库表填充ASP.NET MVC中的DropDownList的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!