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的更多相关文章

随机推荐

  1. shell编程——if语句 if -z -n -f -eq -ne -lt

    if  条件then Commandelse Commandfi                              别忘了这个结尾 If语句忘了结尾fitest.sh: line 14: sy ...

  2. Jquery拖拽原理

    /* onmousedown : 选择元素 onmousemove : 移动元素 onmouseup : 释放元素 */ 查看Demo:拖拽图片 function drag(obj) { obj.on ...

  3. CF GYM 100703G Game of numbers

    题意:给n个数,一开始基数为0,用这n个数依次对基数做加法或减法,使基数不超过k且不小于0,输出最远能运算到的数字个数,输出策略. 解法:dp.dp[i][j]表示做完第i个数字的运算后结果为j的可能 ...

  4. spring exception--No unique bean of type

    今天碰到一个问题,就是我现有项目需要加一个定时器任务,我的代码如下: <!-- 每日数据同步 总数监测任务******************begin --> <bean id=& ...

  5. the type initializer for '' threw an exception

    the type initializer for '' threw an exception 问题:程序启动时初始化主窗口类时,弹出该错误.调查:查看类的构造函数是否会有异常抛出.解决:去掉类的构造函 ...

  6. NOIP2007 字符串展开

    .字符串的展开 (expand.pas/c/cpp) [问题描述] 在初赛普及组的“阅读程序写结果”的问题中,我们曾给出一个字符串展开的例子:如果在输入的字符串中,含有类似于“d-h”或“4-8”的子 ...

  7. Microsoft云备份解决方案Azure Backup的常见配置问题

    这篇博客文章有助于解决 Microsoft云备份解决方案(即 Azure Backup)的常见配置问题.客户通常会在安装或注册 Azure Backup时遇到这些问题.以下是有关如何诊断和解决问题的建 ...

  8. javascript设计模式7

    链式调用 (function(){ function _$(els){ //... } _$.prototype={ each:function(fn){ for(var i=0,len=this.e ...

  9. Linux PHP实现仿百度文库预览功能

    1.安装openoffice: tar zxvf OOo_3.3.0_Linux_x86-64_install-rpm-wJRE_zh-CN.tar.gzcd RPEMrpm -ivh *.rpm安装 ...

  10. 关于win10安装VisualSVN遇到的一个问题及解决办法

    问题:在win10系统中安装VisaulSVN遇到问题,错误提示:There is problem with this Windows Installer package. A DLL require ...