简体   繁体   English

如何在asp.net mvc中为html.DropDownList设置值?

[英]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.

相关问题 ASP.NET MVC-将JsonConvert.DeserializeObject转换为Html.DropDownList - ASP.NET MVC - JsonConvert.DeserializeObject to Html.DropDownList 使用JavaScript Asp.Net MVC复制@ Html.DropdownList - Clone @Html.DropdownList using javascript Asp.Net MVC Asp.Net MVC 5 Html.DropDownList无法验证 - Asp.Net MVC 5 Html.DropDownList is not validating Asp.Net MVC html.DropDownList根据所选值执行的操作 - Asp.Net MVC html.DropDownList Action according to selected value @ Html.ListBox()和@ Html.DropDownList()如何在asp.net mvc中保持状态? - How does @Html.ListBox() and @Html.DropDownList() keep the state in asp.net mvc? 在ASP.NET MVC中的html.dropdownlist onchange上填充html.textbox - fill html.textbox upon html.dropdownlist onchange in asp.net mvc 在 html.dropdownlist 的 on-change 事件上从视图 MVC Asp.net 调用特定方法(不是操作) - Calling specific method (not an Action) from view MVC Asp.net on on-change event of html.dropdownlist ASP.NET MVC RC(刷新)中的 Html.DropDownList 未预选项目 - Html.DropDownList in ASP.NET MVC RC (refresh) not pre-selecting item 在Asp.net mvc4应用程序的Html.DropDownList中显示两个字段 - Display two fields in Html.DropDownList within Asp.net mvc4 application 在asp.net mvc4应用程序中的Html.DropDownList中添加一个条件 - Add a condition in Html.DropDownList within asp.net mvc4 application
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM