Asp.net mvc MVC中另一个模型的下拉列表绑定
我是asp.NETMVC新手 我在两个表中有两个表tblEmployee和tblDepartment公共字段是department id。 如果用户创建新员工,他们必须从tblDepartment表中选择他们的部门列表。我从该表中获取部门列表,这没有问题,但是当我在DB中提交表单departmentid时,该表单将为空 模型Asp.net mvc MVC中另一个模型的下拉列表绑定,asp.net-mvc,Asp.net Mvc,我是asp.NETMVC新手 我在两个表中有两个表tblEmployee和tblDepartment公共字段是department id。 如果用户创建新员工,他们必须从tblDepartment表中选择他们的部门列表。我从该表中获取部门列表,这没有问题,但是当我在DB中提交表单departmentid时,该表单将为空 模型 public partial class Department { public Department() { this.tblEmploy
public partial class Department
{
public Department()
{
this.tblEmployees = new HashSet<Employee>();
}
public int Id { get; set; }
public string Name { get; set; }
public virtual ICollection<Employee> tblEmployees { get; set; }
public Employee employee { get; set; }
}
public partial class Employee
{
public int EmployeeId { get; set; }
public string Name { get; set; }
public string Gender { get; set; }
public string City { get; set; }
public Nullable<int> DepartmentId { get; set; }
public virtual Department tblDepartment { get; set; }
public List<Department> deprtment { get; set; }
public virtual string Department { get; set; }
public bool available { get; set; }
}
公共部分课程部
{
公共部门()
{
this.tblEmployees=new HashSet();
}
公共int Id{get;set;}
公共字符串名称{get;set;}
公共虚拟ICollection tblEmployees{get;set;}
公共雇员雇员{get;set;}
}
公共部分类雇员
{
public int EmployeeId{get;set;}
公共字符串名称{get;set;}
公共字符串{get;set;}
公共字符串City{get;set;}
公共可为空的部门ID{get;set;}
公共虚拟部门tblDepartment{get;set;}
公共列表部门{get;set;}
公共虚拟字符串部门{get;set;}
公共bool可用{get;set;}
}
控制器:
public ActionResult Create()
{
SampleDbContext Db = new SampleDbContext();
List<Employee> employees = Db.Employees.Include("tblDepartment").ToList();
ViewBag.list = new SelectList(Db.Departments, "Id", "Name");
return View();
}
[HttpPost]
public ActionResult Create( Employee employee)
{
SampleDbContext Db = new SampleDbContext();
if (ModelState.IsValid)
{
Db.Employees.Add(employee);
Db.SaveChanges();
return RedirectToAction("Index");
}
return View(employee);
}
public ActionResult Create()
{
SampleDbContext Db=新的SampleDbContext();
List employees=Db.employees.Include(“tblDepartment”).ToList();
ViewBag.list=新选择列表(Db.Departments,“Id”,“Name”);
返回视图();
}
[HttpPost]
公共操作结果创建(员工)
{
SampleDbContext Db=新的SampleDbContext();
if(ModelState.IsValid)
{
Db.Employees.Add(员工);
Db.SaveChanges();
返回操作(“索引”);
}
返回视图(员工);
}
视图:
@model EmployeeList.Models.Employee
....
@使用(Html.BeginForm())
{
@Html.AntiForgeryToken()
雇员
@Html.ValidationSummary(true,“,new{@class=“text danger”})
@LabelFor(model=>model.Name,htmlAttributes:new{@class=“controllabel col-md-2”})
@EditorFor(model=>model.Name,new{htmlAttributes=new{@class=“form control”})
@Html.ValidationMessageFor(model=>model.Name,“,new{@class=“text danger”})
....//员工其他属性的控件
@LabelFor(model=>model.DepartmentId,“DepartmentId”,htmlAttributes:new{@class=“controllabel col-md-2”})
@Html.DropDownList(“列表”、“选择Depar”)
@Html.ValidationMessageFor(model=>model.DepartmentId,“,new{@class=“text danger”})
}
非常感谢……它在@stephenmuecket工作非常感谢……它在@StephenMuecke工作
@model EmployeeList.Models.Employee
....
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>Employee</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.Name, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Name, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Name, "", new { @class = "text-danger" })
</div>
</div>
.... // controls for other properties of Employee
<div class="form-group">
@Html.LabelFor(model => model.DepartmentId, "DepartmentId", htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownList("list","select Depar")
@Html.ValidationMessageFor(model => model.DepartmentId, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-default" />
</div>
</div>
</div>
}