将下拉列表与数据库 table 绑定并获取所选项目 n MVC

Binding dropdownlist with database table and get selected item n MVC

我是 asp.net mvc 的新手。

我有一个员工注册视图,其中我有一个部门的 dropdown-list,我想将 dropdown-list 与数据库 table tblDepartment 绑定,我已经做到了。但我不知道如何获取 post-back 列表中的 select 项。

员工控制器

[HttpGet]
public ActionResult AddEmployee()
{
    ViewBag.Departments = new SelectList(_department.GetAll(), "id", "name");
    return View(_employee.Get());
}

[HttpPost]
public ActionResult AddEmployee(FormCollection data)
{
    var emp = _employee.Get();
    emp.name = data["name"];
    emp.age = Convert.ToInt32(data["age"]);
    emp.dept_id = Convert.ToInt32(data["dept_id"]); // return dept_id 0 always.
    emp.city = data["city"];
    _employee.Insert(emp);
    return RedirectToAction("Index");
}

查看

@using (Html.BeginForm()) 
{
    @Html.AntiForgeryToken()

    <div class="form-horizontal">
        <hr />
        @Html.ValidationSummary(true, "", new { @class = "text-danger" })

        <div class="form-group">
            @Html.LabelFor(model => model.name, "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>

        <div class="form-group">
            @Html.LabelFor(model => model.age, "Age", htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.age, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.age, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.dept_id, "Department", htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.DropDownList("Departments", "Select Department")
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.city, "City", htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.city, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.city, "", 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>
}

您正在使用名称 DropDownLists 定义下拉列表,即 @Html.DropDownList("Departments" 这将是下拉列表的名称,因此它将呈现为:

<select name="Departments">
</select>

并且在表单 post 中,值是 post 键值对,键是输入元素的名称。

但是在 post 请求中,您在 dept_id 中找到 posted 值,您需要从 FormCollection 即:

emp.dept_id = Convert.ToInt32(data["Departments"]);

旁注:

您应该使用强类型助手将 post 值直接用于建模并让模型绑定器为您施展魔法,而不是手动填充模型。

为此,您的辅助方法将是 DropDownListFor :

@Html.DropDownListFor(x=>x.dept_id ,ViewBag.Departments as SelectList, "Select Department")

并在操作方法中将参数设置为模式对象:

[HttpPost]
public ActionResult AddEmployee(Employee employee)
{
     If(ModelState.IsValid)
     {
        _employee.Insert(employee);
        return RedirectToAction("Index");
     }
     else
     {
         return View(employee);
     }
}