Asp.net mvc 将dropdownlist与数据库表绑定并从MVC中获取所选项

Asp.net mvc 将dropdownlist与数据库表绑定并从MVC中获取所选项,asp.net-mvc,html-select,dropdownlistfor,Asp.net Mvc,Html Select,Dropdownlistfor,我是asp.NETMVC新手 我有一个员工注册视图,其中有一个部门的下拉列表,我想将下拉列表绑定到数据库表tblDepartment,我已经完成了。但是我不知道如何在post back上获取列表中的select项 员工控制员 [HttpGet] public ActionResult AddEmployee() { ViewBag.Departments = new SelectList(_department.GetAll(), "id", "name"); return Vi

我是asp.NETMVC新手

我有一个员工注册视图,其中有一个部门的
下拉列表
,我想将
下拉列表
绑定到数据库表
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>
}
@使用(Html.BeginForm())
{
@Html.AntiForgeryToken()

@Html.ValidationSummary(true,“,new{@class=“text danger”}) @LabelFor(model=>model.name,“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.age,“age”,htmlAttributes:new{@class=“controllabel col-md-2”}) @EditorFor(model=>model.age,new{htmlAttributes=new{@class=“form control”}) @Html.ValidationMessageFor(model=>model.age,“,new{@class=“text danger”}) @LabelFor(model=>model.dept_id,“Department”,htmlAttributes:new{@class=“controllabel col-md-2”}) @Html.DropDownList(“部门”、“选择部门”) @LabelFor(model=>model.city,“city”,htmlAttributes:new{@class=“controllabel col-md-2”}) @EditorFor(model=>model.city,new{htmlAttributes=new{@class=“form control”}) @Html.ValidationMessageFor(model=>model.city,“,new{@class=“text danger”}) }
您正在使用名称DropDownLists定义您的dropdownlist,即
@Html.dropdownlist(“部门”
),它将是下拉列表的名称,因此呈现如下:

<select name="Departments">
</select>
旁注: 您应该使用强类型助手将值直接发布到模型中,并让模型绑定器为您发挥作用,而不是手动填充模型

为此,您的助手方法将是:

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

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

您正在使用名称DropDownLists定义您的dropdownlist,即
@Html.dropdownlist(“部门”
),这将是下拉列表的名称,因此它将呈现为:

<select name="Departments">
</select>
旁注: 您应该使用强类型助手将值直接发布到模型中,并让模型绑定器为您发挥作用,而不是手动填充模型

为此,您的助手方法将是:

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

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