C#_dropdownlist_2
string deptId =Request.Form["depts"].Trim();
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();
}
- 原型一:
public static string DropDownList(this HtmlHelper htmlHelper, string name)
{
IEnumerable<SelectListItem> selectData = htmlHelper.GetSelectData(name);
return htmlHelper.SelectInternal(null, name, selectData, true, false, null);
}
第一种方式: List<SelectListItem> items = new List<SelectListItem>();
items.Add(new SelectListItem() { Text = "001", Value = "1", Selected = false });
items.Add(new SelectListItem() {Text = "002", Value = "2", Selected = false });
ViewData["items"] = items;
简化后:
var items = new List<SelectListItem>()
{
(new SelectListItem() {Text = "001", Value = "1", Selected = false}),
(new SelectListItem() {Text = "002", Value = "2", Selected = false})
};
将items值给ViewData:
ViewData["items"] = items;
在aspx中这样使用:
<%= Html.DropDownList("items") %>
生成的代码中,items将作为<select>标签的name和id值。
- 原型二:
public static string DropDownList(this HtmlHelper htmlHelper, string name, IEnumerable<SelectListItem> selectList) { return htmlHelper.DropDownList(name, selectList, null); }
使用方法:
<%= Html.DropDownList("items", new List<SelectListItem> { (new SelectListItem() {Text = "001", Value = "1", Selected = false}), (new SelectListItem() {Text = "002", Value = "2", Selected = false}) })%>
在这里,不需要ViewData传入值,第一个参数items作为标签的name和id的值。items也可以是任意的字符串。
- 原型三
public static string DropDownList(this HtmlHelper htmlHelper, string name, string optionLabel) { IEnumerable<SelectListItem> selectData = htmlHelper.GetSelectData(name); return htmlHelper.SelectInternal(optionLabel, name, selectData, true, false, null); }
使用方法和第一种原型相同,string optionLabel作为一个缺省的空的选项。这样可以完成加入不需要选取任何选项的场景。
C#_dropdownlist_2的更多相关文章
随机推荐
- Ruby基础类型,动态特性,代码块
#Ruby内置基础数据类型 NilClass,TureClass,FalseClass,Time,Date,String,Range,Struct,Array,Hash #Numerice 1.分为I ...
- Android学习系列(1)--为App签名(为apk签名)
写博客是一种快乐,前提是你有所写,与人分享,是另一种快乐,前提是你有舞台展示,博客园就是这样的舞台.这篇文章是android开发人员的必备知识,是我特别为大家整理和总结的,不求完美,但是有用. 1.签 ...
- MySQL table_id原理及风险分析
1. 什么是table_id MySQL binlog文件按格式分为文件头部和事件信息.文件头部占4字节,内容固定为:"\xfe\x62\x69\x6e",接下来就是各个event ...
- android广告平台介绍
广告模式: 广告条:最普遍的广告模式,嵌入在应用界面内,用户点击行为会带来收入. 积分墙:应用通过限制功能.去广告等引导用户进入积分墙页面下载广告应用得到积分来换取使用的模式,用户安装完推荐广 ...
- Kettle简介
ETL和Kettle简介 ETL即数据抽取(Extract).转换(Transform).装载(Load)的过程.它是构建数据仓库的重要环节.数据仓库是面向主题的.集成的.稳定的且随时间不断变 ...
- 什么是MBeanServer
什么是MBeanServer MBeanServer是一个包含所有注册MBean的仓库.它是JMX代理层的核心.JMX1.0规范提供一个接口叫 javax.management.MBeanServer ...
- 16、传感器(Sensor)
一.什么是传感器 传感器是一种物理装置或生物器官,能够探测.感受外界的信号.物理条件(如光.热.湿度)或化学组成(如烟雾),并将探知的信息传递给其他装置或器官.国家标准GB7665—87对传感器的定义 ...
- Hadoop2.7.2安装笔记
1.设置免密SSH登录 $ ssh-keygen -t dsa -P '' -f ~/.ssh/id_dsa $ cat ~/.ssh/id_dsa.pub >> ~/.ssh/autho ...
- CCF 认证4
题意:求强联通分量 Tarjan算法 #include<iostream> #include<stdio.h> #include<stdlib.h> #includ ...
- 在KVM虚拟机中使用spice系列之二(USB映射,SSL,密码,多客户端支持)
在KVM虚拟机中使用spice系列之二(USB映射,SSL,密码,多客户端支持) 发布时间: 2015-02-27 00:16 1.spice的USB重定向 1.1 介绍 使用usb重定向,在clie ...