简体   繁体   English

在下拉列表中选择选定的值

[英]Select selected value in drop down list

I have an enum which looks something like 我有一个看起来像的enum

public enum Department
{
    Admin,
    HR,
    Marketing,
    Sales
}

From this I am trying to create a drop down list in the controller and I do it like 由此,我试图在控制器中创建一个下拉列表,我这样做

public SelectList GetDepartmentDropDownList(string departmentName = null)
{
    var departments = Enum.GetValues(typeof(Department));
    SelectList ddl = new SelectList(departments);

    return ddl;
}

Which works fine. 哪个工作正常。 But as you can see I can pass in the opitional parameter. 但是正如您所看到的,我可以传入opitional参数。 This optional parameter is passed in when the dropdown value is saved to the database and the user comes back to edit the section. 当下拉值保存到数据库中并且用户回来编辑该部分时,将传递此可选参数。 What I am trying to achieve is whatever the user originally selected, when they come back on the edit screen that particular item is select ie if they selected HR when they come back HR should be selected. 我要实现的目标是用户最初选择的内容,当他们回到编辑屏幕时,即选择了特定项目,即,如果他们返回时选择了HR ,则应该选择HR

If I do new SelectList(departments, departmentName, departmentName); 如果我做new SelectList(departments, departmentName, departmentName); I get the following error: 我收到以下错误:

DataBinding: Enums.Department' does not contain a property with the name 'HR' 数据绑定:Enums.Department'不包含名称为'HR'的属性

Can someone suggest how to achieve this please. 有人可以建议如何实现这一目标。

In the view I am making using of Html.DropDownListFor() as 在视图中,我正在使用Html.DropDownListFor()作为

@Html.DropDownListFor(m => m.Department, Model.DepartmentDdl, "Please select a Department", new { @class = "form-control", @required = "required" })

The property in my model is 我模型中的属性是

public IEnumerable<SelectListItem> DepartmentDdl { get; set; }

And in the controller create action I do 然后在控制器中创建动作

model.DepartmentDdl = GetDepartmentDropDownList();

And in the controller edit action I do 在控制器编辑操作中

model.DepartmentDdl = GetDepartmentDropDownList(departmentName); //department name here is read from the db

Model binding works by binding to the value of your property. 模型绑定通过绑定到属性值来起作用。 You need to set the value of property Department in the GET method before you pass the model to the view, for example 例如,您需要在GET方法中设置属性Department的值,然后再将模型传递给视图。

var model = new YourModel
{
    Department = Department.HR,
    DepartmentDdl = GetDepartmentDropDownList()
};
return View(model);

Note that there is no need for your parameter in the GetDepartmentDropDownList() method. 请注意,在GetDepartmentDropDownList()方法中不需要您的参数。 Internally, the DropDownListFor() method builds a new IEnumerable<SelectListItem> and sets the Selected value of each SelectListItem based on the property your binding to (ie setting it in the SelectList constructor is ignored). 在内部, DropDownListFor()方法将构建新的IEnumerable<SelectListItem>并根据绑定到的属性设置每个SelectListItemSelected值(即,将其忽略在SelectList构造函数中进行设置)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM