简体   繁体   English

如何在 ASP.NET 的 DropDownList 中设置选定的等于 True

[英]How can I set selected equals to True in a DropDownList in ASP.NET

I have this code:我有这个代码:

@Html.DropDownList("ddlTiposUsuarios", Model.tiposUsuarios.Select(item => new SelectListItem
                               {
                                   Value = item.Id_Tipo_Usuario.ToString(),
                                   Text = item.Nombre_Tipo_Usuario.ToString(),
                                   Selected = "select" == item.Id_Tipo_Usuario.ToString()
                               }), new { @class = "form-select", aria_label = "Default select eaxmple" })

I retrieve a list of four elements from SQL Server and I want to set an option html element which "selected" attribute equals true, depending query from Controller.我从 SQL 服务器检索四个元素的列表,我想设置一个选项 html 元素,其中“选定”属性等于 true,具体取决于来自 Controller 的查询。 Any tips?有小费吗?

Instead of creating the SelectList on your View , create the list in your Controller and set it to a ViewBag and see how easy it becomes after that:与其在View上创建SelectList ,不如在Controller中创建列表并将其设置为ViewBag ,然后看看它变得多么容易:

In your Controller , do this:在您的Controller中,执行以下操作:

ViewBag.myddlItems = tiposUsuarios.Select(item => new SelectListItem
                               {
                                   Value = item.Id_Tipo_Usuario.ToString(),
                                   Text = item.Nombre_Tipo_Usuario.ToString()
                               });

and then in your View, you can construct your DropDownList using the ViewBag :然后在您的视图中,您可以使用ViewBag构建DropDownList

<select id="ddlTiposUsuarios" class="form-select" aria_label = "Default select eaxmple">
    @foreach (SelectListItem option in ViewBag.myddlItems)
    {
        <option value="@option.Value" @(option.Value == "select" ? "selected='selected'" : "")>@option.Text</option>
    }
</select>

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

相关问题 如何在asp.net mvc 4中设置下拉列表的选定值 - How can I set the selected value of dropdownlist in asp.net mvc 4 如何在ASP.NET中设置一个下拉列表项? - How to set a dropdownlist item as selected in ASP.NET? 如何在asp.net mvc3中切换出具有True和False且未设置CheckBox的DropDownList? - How do I switch out a DropDownList with True and False and Not Set with a CheckBox in asp.net mvc3? 如何获取DropDownList选定的值,并将其发送到ASP.NET MVC 4中的控制器并使用JavaScript? - How can I get a DropDownList selected value and send it to the controller in ASP.NET MVC 4 and using JavaScript? 如何将我在中继器中选择的 DropDownList 值添加到 ASP.Net 中的列表框控件 - How Can add I selected value of DropDownList inside a Repeater to the listbox control in ASP.Net 在下拉列表中查找项目并将其设置为.asp.net MVC 4中的选定项 - find item in dropdownlist and set it to selected in .asp.net MVC 4 在 ASP.Net 中的 GridView EditItemTemplate 中设置 DropDownList 选定值 - Set DropDownList Selected Value in GridView EditItemTemplate in ASP.Net asp.net:如何从下拉列表中删除项目? - asp.net: How can I remove an item from a dropdownlist? 如何在ASP.NET中禁用下拉列表? - How can I disable a dropdownlist in ASP.NET? 我如何检查一个DropDownList包含asp.net中的项目 - How can I check a DropDownList for containing item in asp.net
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM