在ASP.NET MVC中填充DropDownList

时间:2018-12-17 06:42:25

标签: c# asp.net-mvc model-view-controller

以下代码是创建用户的方法。我有一个用户类,我需要在这里创建用户。但是我在一个人身上有部门编号,该部门编号引用数据库中另一个名为部门的表。

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>

这个model.department部分是我丢失的地方。我需要按列表顺序列出我的部门,当用户选择时,我想选择部门的ID。 像这样;

enter image description here

所以我希望用户看到

  

部门名称+子部门名称

,当他们从列表中进行选择时,所选择的是

  

部门编号

所以我可以这样添加数据库。

这是我的“创建帖子”方法:

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

     return View(user);
 }

她是我的User类;

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

    public int DepartmentId { get; set; }

    // Other fields removed for brevity
}

这里是Department类;

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

    public string DepartmentName { get; set; }

    public string SubDepartmentName { get; set; }
}

1 个答案:

答案 0 :(得分:3)

编写如下的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();
}

然后在视图中:

<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 POST方法。您不允许DepartmentId传递Bind包含列表。请更新您的Create POST方法,如下所示:

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);
}