动态下拉列表不返回 MVC 中的任何值

Dynamic DropDown List not returning any value in MVC

大家好,我需要你的帮助,我在 MVC 中有一个创建员工的表单。我从下拉列表中填写表单和 select,当我单击提交时,每个其他字段 returns 一个值,除了下拉列表。我似乎无法解决问题。这是表格的样子。

这是 CreateEmployee 模型

    public class CreateEmployee
{
    public int Id { get; set; }
    public string FullName { get; set; }
    [Required]
    public string Notes { get; set; }

    public int Department { get; set; }
    public IEnumerable<AngTestDepartment> Departments { get; set; }

    public bool PerkCar { get; set; }
    public bool PerkStock { get; set; }
    public bool PerkSixWeeks { get; set; }
    public string PayrollType { get; set; }
}

动作控制器

public ActionResult Employee(){
using (var db = new DepartmentDbContext())
{
    var model = new CreateEmployee();
    model.Departments = db.AngTestDepartments.ToList();
    return View(model);
} } 

和视图

@using (Html.BeginForm("CreateEmployee", "Employee", FormMethod.Post))

{ @Html.AntiForgeryToken()

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

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

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

    // the drop down  doesnt return a value on submit
    <div class="form-group">
        @Html.LabelFor(model => model.Department, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">

            @Html.DropDownListFor(m => m.Departments,
                Model.Departments.Select(d => new SelectListItem()
                                                {
                                                    Value = d.id.ToString(),
                                                    Text = d.Department
                                                }
                                    ), new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.Department, "", new { @class = "text-danger" })

        </div>
    </div> }   

和提交时调用的操作结果

        public ActionResult CreateEmployee(CreateEmployee newEmployee)
    {
        DbEmployeeTable.DbEmployeeTable_EmployeeTable(newEmployee);
        return RedirectToAction("Employees");
    }

非常感谢您的帮助。

而不是

@Html.DropDownListFor(m => m.Departments,

试试这个...

@Html.DropDownListFor(m => m.Department,

那应该 return 部门 ID 作为所选值的整数。

将下拉列表代码更改为:

@Html.DropDownListFor(m => m.Department, new SelectList(Model.Departments.Select(d => new SelectListItem()
    {
        Value = d.id.ToString(),
        Text = d.Department
    }
)), new { htmlAttributes = new { @class = "form-control" } })

Dropdownlistfor 的第一个参数应该是您要获取的值,第二个参数是 select 列表可以避免您以后与持久性相关的麻烦。

顺便说一句,我还会将 Model.Departments.Select 结果设为一个模型 属性,这样您就不会把视图弄得太乱,但这只是一个风格问题。