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的更多相关文章
随机推荐
- 【 D3.js 高级系列 — 3.0 】 堆栈图
堆栈图布局(Stack Layout)能够计算二维数组每一数据层的基线,以方便将各数据层叠加起来.本文讲解堆栈图的制作方法. 先说说什么是堆栈图. 例如,有如下情况: 某公司,销售三种产品:个人电脑. ...
- mysql CMAKE 参数说明
MySQL自5.5版本以后,就开始使用CMake编译工具了,因此,你在安装源文件中找不到configure文件是正常的.很多人下到了新版的MySQL,因为找不到configure文件,不知道该怎么继续 ...
- cdn是什么和作用有些
内容分发网络其基本思路是尽可能避开互联网上有可能影响数据传输速度和稳定性的瓶颈和环节,使内容传输的更快.更稳定.通过在网络各处放置节点服务器所构 成的在现有的互联网基础之上的一层智能虚拟网络,CDN系 ...
- bzoj 1058 [ZJOI2007]报表统计(set)
[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=1058 [题意] 一个序列,提供插入,查询相邻最小差值,查询任意最小差值的操作. [思路 ...
- 【原创】_INTSIZEOF 内存按照int对齐
#include <stdarg.h> 里面定义了如下宏 #define _INTSIZEOF(n) ( (sizeof(n) + sizeof(int) - 1) & ~(siz ...
- HW7.10
public class Solution { public static void main(String[] args) { int[][] array = new int[3][3]; for( ...
- POJ3468--A Simple Problem with Integers(Splay Tree)
虽然有点难,但是这套题都挂了一个月了啊喂…… 网上模板好多……最后还是抄了kuangbin聚聚的,毕竟好多模板都是抄他的,比较习惯…… POJ 3468 题意:给n个数,两种操作,区间整体加一个数,或 ...
- Oracle-查看oracle是否有表被锁
问题现象: 查看oracle是否有表被锁 解决方法: select sid,serial#,program,terminal,username,b.object_id,c.object_name f ...
- python 面向对象高级编程
数据封装.继承和多态只是面向对象程序设计中最基础的3个概念.在Python中,面向对象还有很多高级特性,允许我们写出非常强大的功能. 我们会讨论多重继承.定制类.元类等概念.
- 利用.htacess 实现重定向
步骤: 在网站目录下加入 .htaccess 文件中写 RewriteEngine On RewriteRule ^.*$ index.php 表示开启重写机制 重写到 index.php 的文件