[英]how to set a value for the html.DropDownList in asp.net mvc?
how to set a value for the html.dropdownlist in asp.net mvc for example, suppose that we have this list 例如,如何在asp.net mvc中设置html.dropdownlist的值,假设我们有此列表
@Html.DropDownList("Suppliers", Model.PDTO.ProjectSuppliers, new { @class = "text_field multiList hide", @multiple = "multiple" })*@
I know that I can use ListBoxFor or DropDownListFor Like this 我知道我可以像这样使用ListBoxFor或DropDownListFor
@Html.ListBoxFor(model => model.Suppliers, Model.PDTO.ProjectSuppliers, new { @class = "text_field multiList hide", @multiple = "multiple" })
but I am wondering about how can we do this with the DropDownList, I tried to search in google but I found answers just for DropDownListFor
. 但是我想知道如何使用DropDownList做到这一点,我试图在google中搜索,但是找到了仅适用于
DropDownListFor
答案。
Any help please. 请帮忙。
DropDownList is not strongly typed. DropDownList的类型不强。 To get selected options for DropDownList you should write code in your action.
要获得DropDownList的选定选项,您应该在操作中编写代码。
The following code snippet may help you. 以下代码段可能会对您有所帮助。
In your action: 在您的行动中:
var selectedValue = new List<int> {1, 2};
ViewBag.ProjectSuppliers= new MultiSelectList(SupplierList, "Id", "SupplierName", selectedValue );
In you view: 在您看来:
@Html.DropDownList("Suppliers", (MultiSelectList)ViewBag.ProjectSuppliers, "-----Select-----", new { multiple="" })
Not sure if this is what you mean, but I know I've used dropdownlist before by loading up an 不确定这是否是您的意思,但是我知道我之前已经通过加载下拉列表使用dropdownlist
IEnumerable<SelectListItems> selectList
in the controller, passing it to the ViewBag 在控制器中,将其传递给ViewBag
ViewBag.selectList = selectList;
and then just using 然后使用
@Html.DropDownList("selectList")
in the view. 在视图中。
There are overloads for it if you choose to go another route though. 如果您选择走另一条路线,则会有很多负担。 https://msdn.microsoft.com/en-us/library/system.web.mvc.html.selectextensions.dropdownlist(v=vs.118).aspx
https://msdn.microsoft.com/zh-CN/library/system.web.mvc.html.selectextensions.dropdownlist(v=vs.118).aspx
See the following for a more detailed example of the ViewBag method: http://www.mikesdotnetting.com/article/128/get-the-drop-on-asp-net-mvc-dropdownlists 请参阅以下有关ViewBag方法的更详细示例: http : //www.mikesdotnetting.com/article/128/get-the-drop-on-asp-net-mvc-dropdownlists
We will have to assign data to dropdownlist from ViewData then we will able to set selected value to dropdownlist, something like this below 我们必须将数据从ViewData分配到下拉列表,然后才能将选定的值设置为下拉列表,如下所示
private ActionResult GetEmployeesForDropDown()
{
var lstEmployees = new[]
{
new Employee { Id = 1, Name = "Emp1" },
new Employee { Id = 2, Name = "Emp2" },
new Employee { Id = 3, Name = "Emp3" }
};
var selectList = new SelectList(lstEmployees, "Id", "Name", 0);
//Here 0 is the selectedIndex which we are assigning to dropdownlist, we can pass value we want here to set the index
ViewData["Employees"] = selectList;
// dropdownlist datasource which is a employeelist is assigned to a ViewData here
return View();
}
View will look like this 视图将如下所示
@Html.DropDownList("ddEmployees", (SelectList)ViewData["Employees"], "---Select---", new {@class="DropDownCssIfAny"})
Controller: 控制器:
List<Department> departments = dbHR.Departments.OrderBy(d => d.DEPARTMENT_NAME).ToList();
departmentsList = new SelectList(departments, "DEPARTMENT_ID", "DEPARTMENT_NAME", deptInt);
ViewBag.DepartmentsList = departmentsList;
View: 视图:
<%: Html.DropDownList("DEPARTMENTS", (IEnumerable<SelectListItem>)ViewBag.DepartmentsList, string.Empty)%>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.