【问题标题】:Binding dropdownlist with database table and get selected item n MVC将下拉列表与数据库表绑定并在 MVC 中获取选定项
【发布时间】:2017-04-03 16:28:49
【问题描述】:

我是 asp.net mvc 的新手。

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

员工控制人

[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>
}

【问题讨论】:

    标签: asp.net-mvc html-select dropdownlistfor


    【解决方案1】:

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

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

    在表单中,值以键值对的形式发布,键是输入元素的名称。

    但在发布请求中,您在 dept_id 中找到发布的值,您需要从 FormCollection 中的正确键读取,即:

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

    旁注:

    您应该使用强类型帮助器将值直接发布到模型并让模型绑定器为您发挥作用,而不是手动填充模型。

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

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

    在action方法中将参数设置为模式对象:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多