C# 在ASP.NET MVC中填充DropDownList

C# 在ASP.NET MVC中填充DropDownList,c#,asp.net-mvc,model-view-controller,C#,Asp.net Mvc,Model View Controller,下面的代码用于创建用户。我有一个用户类,我需要在这里创建用户。但我有一个人的部门id,该部门id引用数据库中另一个名为department的表 public ActionResult Create() { // disable lazy because of error it creates _db.Configuration.LazyLoadingEnabled = false; var data = _db.Departments.OrderBy(a => a.

下面的代码用于创建用户。我有一个用户类,我需要在这里创建用户。但我有一个人的部门id,该部门id引用数据库中另一个名为department的表

public ActionResult Create()
{
    // disable lazy because of error it creates
    _db.Configuration.LazyLoadingEnabled = false;
    var data = _db.Departments.OrderBy(a => a.DepartmentId).ToList();
    ViewData["DepartmentList"] = data;

    return View();
}
以下是我的看法:

@{
    var departmentLists = ViewData["DepartmentList"]; // Cast the list
}

<div class="form-group">
     @Html.LabelFor(model => model.Department, htmlAttributes: new { @class = "control-label col-md-2" })
     <div class="col-md-10">
          @Html.EnumDropDownListFor(model => model.Department, htmlAttributes: new { @class = "form-control" })
         @Html.ValidationMessageFor(model => model.Department, "", new { @class = "text-danger" })
     </div>
</div>
她是我的用户类

public class User
{
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int Id { get; set; }

    public int DepartmentId { get; set; }

    // Other fields removed for brevity
}
这里是
部门
班级

public class Department
{
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int DepartmentId { get; set; }

    public string DepartmentName { get; set; }

    public string SubDepartmentName { get; set; }
}

编写
Create
GET方法,如下所示:

public ActionResult Create()
{
    // disable lazy because of error it creates
    _db.Configuration.LazyLoadingEnabled = false;

    var departmentList = _db.Departments.Select(d => new
     {
        d.DepartmentId,
        DepartmentName = d.DepartmentName + " " + d.SubDepartmentName
     }).OrderBy(a => a.DepartmentId).ToList();

    ViewBag.DepartmentList = new SelectList(departmentList,"DepartmentId","DepartmentName");

    return View();
}
public ActionResult Create([Bind(Include = "ID,LastName,FirstMidName,EnrollmentDate,Email,DepartmentId,Position,Active")] User user)
{
    if (ModelState.IsValid)
    {
       _db.Users.Add(user);
       _db.SaveChanges();
       return RedirectToAction("Index");
    }

    return View(user);
} 
然后在视图中:

<div class="form-group">
        @Html.LabelFor(model => model.DepartmentId, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.DropDownList("DepartmentId", ViewBag.DepartmentList as SelectList, htmlAttributes: new { @class = "form-control" })
            @Html.ValidationMessageFor(model => model.DepartmentId, "", new { @class = "text-danger" })
        </div>
    </div>

编写
Create
GET方法,如下所示:

public ActionResult Create()
{
    // disable lazy because of error it creates
    _db.Configuration.LazyLoadingEnabled = false;

    var departmentList = _db.Departments.Select(d => new
     {
        d.DepartmentId,
        DepartmentName = d.DepartmentName + " " + d.SubDepartmentName
     }).OrderBy(a => a.DepartmentId).ToList();

    ViewBag.DepartmentList = new SelectList(departmentList,"DepartmentId","DepartmentName");

    return View();
}
public ActionResult Create([Bind(Include = "ID,LastName,FirstMidName,EnrollmentDate,Email,DepartmentId,Position,Active")] User user)
{
    if (ModelState.IsValid)
    {
       _db.Users.Add(user);
       _db.SaveChanges();
       return RedirectToAction("Index");
    }

    return View(user);
} 
然后在视图中:

<div class="form-group">
        @Html.LabelFor(model => model.DepartmentId, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.DropDownList("DepartmentId", ViewBag.DepartmentList as SelectList, htmlAttributes: new { @class = "form-control" })
            @Html.ValidationMessageFor(model => model.DepartmentId, "", new { @class = "text-danger" })
        </div>
    </div>

在viewmodel类中,
Department
属性是否具有
enum
类型?如果您想显示连接的文本值,我想您应该尝试使用
DropDownListFor
。@TetsuyaYamamoto我不确定要显示什么,这就是为什么我也问这个问题的原因。
Department
属性是否在viewmodel类中具有
enum
类型?如果你想显示连接的文本值,我想你应该试试
DropDownListFor
。@TetsuyaYamamoto我不确定要显示什么,这就是为什么我也问这个问题。几乎是真的,只是它没有将我们选择的部门id发布到数据库中。请进一步解释这个问题。我会修好的!将您的答案编辑为工作版本,并在我的问题中添加了用户类。一切正常,只是在我创建用户并发布值时,数据库没有收到1个值作为部门ID。不,抱歉,发生了误解。仍然发布部门ID不起作用。其余的没问题。你能给我远程访问吗!几乎正确,只是它没有将我们选择的部门id发布到数据库中。请进一步解释这个问题。我会修好的!将您的答案编辑为工作版本,并在我的问题中添加了用户类。一切正常,只是在我创建用户并发布值时,数据库没有收到1个值作为部门ID。不,抱歉,发生了误解。仍然发布部门ID不起作用。其余的没问题。你能给我远程访问吗!