Html.DropDownList()的用法

JavaScript与下拉列表(DropDownList)的区别分析 通过JavaScript,我们可以实现更复杂的操作和动态效果,以及监听下拉列表的变化并执行相应的操作。我们可以使用JavaScript动态地添加、删除和修改下拉列表的选项,以及根据用户的选择执行相应的操作。JavaScript是一种用于网页开发的脚本语言,而下拉列表(DropDownList)是一种HTML元素,用于在网页中创建可选择的选项列表。相比之下,下拉列表是HTML的一部分,几乎所有的现代浏览器都支持下拉列表的渲染和基本功能。下拉列表是HTML的一部分,用于在网页中创建可选择的选项列表。 阅读详情

源码

using System.Collections;
using System.Diagnostics.CodeAnalysis;

namespace System.Web.Mvc
{
    [SuppressMessage("Microsoft.Naming", "CA1710:IdentifiersShouldHaveCorrectSuffix", Justification = "This is a shipped API")]
    public class SelectList : MultiSelectList
    {
        public SelectList(IEnumerable items)
            : this(items, selectedValue: null)
        {
        }

        public SelectList(IEnumerable items, object selectedValue)
            : this(items, dataValueField: null, dataTextField: null, selectedValue: selectedValue)
        {
        }

        /// <summary>
        /// Initializes a new instance of the SelectList class by using the specified items for the list,
        /// the selected value, and the disabled values.
        /// </summary>
        /// <param name="items">The items used to build each <see cref="SelectListItem"/> of the list.</param>
        /// <param name="selectedValue">The selected value. Used to match the Selected property of the corresponding
        /// <see cref="SelectListItem"/>.</param>
        /// <param name="disabledValues">The disabled values. Used to match the Disabled property of the corresponding
        /// <see cref="SelectListItem"/>.</param>
        public SelectList(IEnumerable items, object selectedValue, IEnumerable disabledValues)
            : this(items,
                   dataValueField: null,
                   dataTextField: null,
                   selectedValue: selectedValue,
                   disabledValues: disabledValues)
        {
        }

        public SelectList(IEnumerable items, string dataValueField, string dataTextField)
            : this(items, dataValueField, dataTextField, selectedValue: null)
        {
        }

        public SelectList(IEnumerable items, string dataValueField, string dataTextField, object selectedValue)
            : base(items, dataValueField, dataTextField, ToEnumerable(selectedValue))
        {
            SelectedValue = selectedValue;
        }

        /// <summary>
        /// Initializes a new instance of the SelectList class by using the specified items for the list,
        /// the data value field, the data text field, the data group field, and the selected value.
        /// </summary>
        /// <param name="items">The items used to build each <see cref="SelectListItem"/> of the list.</param>
        /// <param name="dataValueField">The data value field. Used to match the Value property of the corresponding
        /// <see cref="SelectListItem"/>.</param>
        /// <param name="dataTextField">The data text field. Used to match the Text property of the corresponding
        /// <see cref="SelectListItem"/>.</param>
        /// <param name="dataGroupField">The data group field. Used to match the Group property of the corresponding
        /// <see cref="SelectListItem"/>.</param>
        /// <param name="selectedValue">The selected value. Used to match the Selected property of the corresponding
        /// <see cref="SelectListItem"/>.</param>
        public SelectList(IEnumerable items,
                          string dataValueField,
                          string dataTextField,
                          string dataGroupField,
                          object selectedValue)
            : base(items, dataValueField, dataTextField, dataGroupField, ToEnumerable(selectedValue))
        {
            SelectedValue = selectedValue;
        }

        /// <summary>
        /// Initializes a new instance of the SelectList class by using the specified items for the list,
        /// the data value field, the data text field, the selected value, and the disabled values.
        /// </summary>
        /// <param name="items">The items used to build each <see cref="SelectListItem"/> of the list.</param>
        /// <param name="dataValueField">The data value field. Used to match the Value property of the corresponding
        /// <see cref="SelectListItem"/>.</param>
        /// <param name="dataTextField">The data text field. Used to match the Text property of the corresponding
        /// <see cref="SelectListItem"/>.</param>
        /// <param name="selectedValue">The selected value. Used to match the Selected property of the corresponding
        /// <see cref="SelectListItem"/>.</param>
        /// <param name="disabledValues">The disabled values. Used to match the Disabled property of the corresponding
        /// <see cref="SelectListItem"/>.</param>
        public SelectList(IEnumerable items,
                          string dataValueField,
                          string dataTextField,
                          object selectedValue,
                          IEnumerable disabledValues)
            : base(items, dataValueField, dataTextField, ToEnumerable(selectedValue), disabledValues)
        {
            SelectedValue = selectedValue;
        }

        /// <summary>
        /// Initializes a new instance of the SelectList class by using the specified items for the list,
        /// the data value field, the data text field, the data group field, the selected value, and the disabled
        /// values.
        /// </summary>
        /// <param name="items">The items used to build each <see cref="SelectListItem"/> of the list.</param>
        /// <param name="dataValueField">The data value field. Used to match the Value property of the corresponding
        /// <see cref="SelectListItem"/>.</param>
        /// <param name="dataTextField">The data text field. Used to match the Text property of the corresponding
        /// <see cref="SelectListItem"/>.</param>
        /// <param name="dataGroupField">The data group field. Used to match the Group property of the corresponding
        /// <see cref="SelectListItem"/>.</param>
        /// <param name="selectedValue">The selected value. Used to match the Selected property of the corresponding
        /// <see cref="SelectListItem"/>.</param>
        /// <param name="disabledValues">The disabled values. Used to match the Disabled property of the corresponding
        /// <see cref="SelectListItem"/>.</param>
        public SelectList(IEnumerable items,
                          string dataValueField,
                          string dataTextField,
                          string dataGroupField,
                          object selectedValue,
                          IEnumerable disabledValues)
            : base(items, dataValueField, dataTextField, dataGroupField, ToEnumerable(selectedValue), disabledValues)
        {
            SelectedValue = selectedValue;
        }

        /// <summary>
        /// Initializes a new instance of the SelectList class by using the specified items for the list,
        /// the data value field, the data text field, the data group field. the selected value, the disabled values,
        /// and the disabled groups.
        /// </summary>
        /// <param name="items">The items used to build each <see cref="SelectListItem"/> of the list.</param>
        /// <param name="dataValueField">The data value field. Used to match the Value property of the corresponding
        /// <see cref="SelectListItem"/>.</param>
        /// <param name="dataTextField">The data text field. Used to match the Text property of the corresponding
        /// <see cref="SelectListItem"/>.</param>
        /// <param name="dataGroupField">The data group field. Used to match the Group property of the corresponding
        /// <see cref="SelectListItem"/>.</param>
        /// <param name="selectedValue">The selected value. Used to match the Selected property of the corresponding
        /// <see cref="SelectListItem"/>.</param>
        /// <param name="disabledValues">The disabled values. Used to match the Disabled property of the corresponding
        /// <see cref="SelectListItem"/>.</param>
        /// <param name="disabledGroups">The disabled groups. Used to match the Disabled property of the corresponding
        /// <see cref="SelectListGroup"/>.</param>
        public SelectList(IEnumerable items,
                          string dataValueField,
                          string dataTextField,
                          string dataGroupField,
                          object selectedValue,
                          IEnumerable disabledValues,
                          IEnumerable disabledGroups)
            : base(items,
                   dataValueField,
                   dataTextField,
                   dataGroupField,
                   ToEnumerable(selectedValue),
                   disabledValues,
                   disabledGroups)
        {
            SelectedValue = selectedValue;
        }

        public object SelectedValue { get; private set; }

        private static IEnumerable ToEnumerable(object selectedValue)
        {
            return (selectedValue != null) ? new object[] { selectedValue } : null;
        }
    }
}

SelectListItem.cs

namespace System.Web.Mvc
{
    public class SelectListItem
    {
        /// <summary>
        /// Gets or sets a value that indicates whether this <see cref="SelectListItem"/> is disabled.
        /// </summary>
        public bool Disabled { get; set; }

        /// <summary>
        /// Represents the optgroup HTML element this item is wrapped into.
        /// In a select list, multiple groups with the same name are supported.
        /// They are compared with reference equality.
        /// </summary>
        public SelectListGroup Group { get; set; }

        public bool Selected { get; set; }

        public string Text { get; set; }

        public string Value { get; set; }
    }
}


Html.DropDownList()赋默认值:

页面代码如下:

<% 
List<SelectListItem> list = new List<SelectListItem> {
new SelectListItem { Text = "启用", Value = "0",Selected = true},
new SelectListItem { Text = "禁用", Value = "1" } };
%>//list储存dropdownlist的默认值

<%=Html.DropDownList("state",list,Model.state) %>  //state为实体的属性,默认选中"启用"

Html.DropDownList()从数据库读取值:

页面代码如下:

<%= Html.DropDownList("Category", ViewData["Categories"] as SelectList,"--请选择--",new { @class = "my-select-css-class" } )
%>

Controllers代码:

public ActionResult Create() 
{
	List<Category> categories = categoryService.GetAll();
	ViewData["Categories"] = new SelectList(categories, "Id", "Name");
	return View();
}

下面是我自己的代码:
页面代码如下:

<!--数据库提取数据 -->
<p>产品分类:<%=Html.DropDownList("category")%></p>
<!--手动设置数据-->
<p>状态:<%=Html.DropDownList("status")%></p>

Controllers代码:

ViewData["category"] = new SelectList(CategoryManager.GetList(),"id","name");

List<SelectListItem> list = new List<SelectListItem> {
 new SelectListItem { Text = "启用", Value = "0",Selected = true},
 new SelectListItem { Text = "禁用", Value = "1" } 
};

ViewData["status"] = list;
MVC4 @Html.DropDownList的用法 ASP .NET MVC4 @Html.DropDownList后台绑定数据展示到页面下拉框中 立即下载

相关推荐

手把手教你用STM32单片机控制双向可控硅调光(附完整代码+电路图)

本文提供了一套完整的STM32单片机控制双向可控硅实现灯光无级调光的实战方案。详细讲解了硬件电路设计,包括过零检测与隔离驱动,并剖析了基于定时器中断的软件核心逻辑,通过精准的相位角计算实现平滑的灯光强度控制。附有完整代码与电路图,适合嵌入式开发者实践。

weixin_29194255的博客 956

DropDownList详解

在.net中,DropDownList和ListBox是最常用的两个LIST控件,我的学习笔记也从这里开始吧! 一、DropDownList 命名空间:System.Web.UI.WebControls 程序集:System.Web(在 system.web.dll 中) 一些常用的属性: DataMember 当数据源包含多个不同的数据项列表时,获取或设置数据绑定控件绑定到的数据列表的

JohnTang2014的专栏 7338

【寄存器模型】二、前门访问与后门访问

1.后门访问可以更好的完成前门访问能够完成的事情:后门访问不消耗仿真时间,且消耗的运行时间也远小于前门访问的运行时间。 2.后门访问可以完成前门访问不能完成的事:对于某些特定的场景,如网络通信系统中的一些只读计数器,当要验证计数器进位归零的场景时,前门访问无法完成写寄存器操作,只能通过消耗仿真时间来使计数器到达特定的计数值。而后门访问可以直接对只读寄存器赋一个初值......

carrotbanana的博客 9657

mvc5html的下拉框,Asp.net MVC5中Html.DropDownList的使用

一.静态下拉列表项的绑定在下拉列表中绑定静态项,我们可以通过 SelectListItem 的集合作为数据源的下拉列表。@Html.DropDownList("dropRoles", new List(){new SelectListItem() { Text= "Yes", Value = "true" },new SelectListItem() { Text= "No", Value = "...

weixin_36393674的博客 1026

从数据到特征:基于CAT12的MRI图像预处理实战指南

本文详细介绍了使用CAT12工具进行MRI图像预处理的完整实战流程。从数据获取、环境搭建到核心的AC-PC校正、组织分割、空间标准化与调制,逐步解析如何将原始脑影像数据转化为可用于机器学习的标准化特征图。重点阐述了预处理作为脑影像分析‘生命线’的重要性,并提供了避坑指南与自动化脚本建议,帮助研究者构建高质量的阿尔茨海默症分类模型输入特征。

weixin_29065959的博客 310

ComfyUI安装包运行前准备:安装Git

Git 是一个分布式版本控制系统(Version Control System, VCS),用于跟踪和管理文件(尤其是代码)的变更历史。 简单理解就是,git是用来管理文件的版本的,如果有新的版本,就可以使用git来更新。

聚梦小课堂 2万+

@Html.DropDownList()的四种用法及自定义DropDownList扩展

常用方法后台代码: public ActionResult Index() { ViewData["deptOu"] = "SOHO"; using (ISession session = new NHibernateHelper(DataBase.ADDB).OpenSession()) { IList<t_data_DeptOU> dep...

xiaouncle的博客 2万+

电磁阀颤振工作原理

颤振 颤振,弹性结构在均匀气(或液)流中受到空气(或液体)动力、弹性力和惯性力(见达朗伯原理)的耦合作用而发生的大幅度振动,它是气动弹性力学中最重要的问题之一。 随着机电液一体化技术的发展,电液比例控制技术的应用越来越广泛。电液比例控制系统范围较广,在液压传动与控制过程中,系统能够接受模拟信号或数字信号,使输出的流量或压力连续成比例地变化,都属于此类。该控制系统的优点是充分利用了液压控制与电气控制的长处,即功率传递靠液压,信号处理靠电气的优化措施。 电液比例控制原理与要求 液压控制系统中两个最重要的被控参数

小幽余生不加糖 1万+

Html.DropDownList()的用法 ( Asp.Net MVC)

Html.DropDownList()赋默认值: 页面代码如下:                   List list = new List {                 new SelectListItem { Text = "启用", Value = "0",Selected = true},                 new SelectListIt

snowalkmount的专栏 1399

Openclaw 附录B 常用Skills清单

核心作用支持URL、网页、PDF、图片、音频和YouTube视频的摘要与字幕提取,由OpenClaw创始人@steipete开发的官方内置Skill。安装⚠️说明:原推荐的summarize未在ClawHub验证到,YouTube字幕提取功能已内置于summarize中。使用场景“提取这个2小时Next.js教程视频的核心代码逻辑,按章节整理成学习笔记”推荐指数:⭐⭐⭐⭐。

Maynor的博客 857

久坐不慌,健康护航 —— 智能震动提醒坐垫的设计与实现

硬件核心:选用 ESP32 为主控,HX711 + 应变片实现精准压力检测,8050 三极管驱动震动马达,锂电池 + TP4056 满足便携供电需求;软件逻辑:核心是 “压力阈值判断 + 时间计时”,达到久坐阈值后触发震动提醒,流程图清晰体现了循环检测、状态重置的核心逻辑;代码特性:代码注释完善,阈值可灵活调整,支持串口调试,新手可直接编译烧录并根据实际场景校准。

weixin_42300449的博客 401

[转]Html.DropDownList()的用法 ( Asp.Net MVC)

Html.DropDownList()赋默认值: 页面代码如下: <% List<SelectListItem> list = new List<SelectListItem> { new SelectListItem { Text = "启用", Value = "0",S...

weixin_30894389的博客 213

25杰瑞机械工程师面试经验机械面试问题总结技巧面试全攻略

作为一个对机械工程充满热情的应届生,我有幸参加了杰瑞集团的机械工程师面试。面试过程既充满挑战,也不乏乐趣。以下是我面试的详细经历,包括面试流程、面试题目及参考答案。 #### 面试流程 1. **HR初面**:主要是对个人背景、教育经历和工作经验的初步了解。 2. **技术面**:由技术部门的主管进行,主要考察专业知识和项目经验。 3. **部门经理面**:更深入地了解我的技术能力和团队合作精神。 4. **总监终面**:最后一轮面试,总监会对候选人的整体素质进行评估。

m0_72216164的博客 2794

Mvc-Html.DropDownList()用法

原型一: public static string DropDownList(this HtmlHelper htmlHelper, string name) { IEnumerable<SelectListItem> selectData = htmlHelper.GetSelectData(name); return htmlHelp...

weixin_34370347的博客 94

迈瑞BS系列全自动生化分析仪LIS协议接口手册(内含HL7和ASTM)

迈瑞BS系列全自动生化分析仪LIS协议接口手册,内含HL7和ASTM)

OV9734 DATASHEET,完整规格书

OV9734完整规格书,包括电气参数,尺寸规格,寄存器配置表等

上一篇: 将Json加载到.NET对象并对结果进行过滤和排序
下一篇: ASP.NET MVC中使用FluentValidation验证实体
.NET跨平台
博客等级 码龄15年 6066粉丝 738原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值