goody9807  
避风的港湾-睿宇科技~~~寻找.Net的精华

可以免费得手机充值卡、QB、8无QQ号、QQ黄钻、QQ绿钻、QQ红钻、游戏点卡、更多实物礼品
公告
日历
<2011年12月>
27282930123
45678910
11121314151617
18192021222324
25262728293031
1234567
统计
  • 随笔 - 570
  • 文章 - 5
  • 评论 - 650
  • 引用 - 114

导航

搜索

 

常用链接

最新随笔

我的标签

随笔分类(565)

我的空间

友情链接

积分与排名

  • 积分 - 521766
  • 排名 - 114

最新评论

推荐排行榜

 

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, truefalsenull);

}

 

 

 

第一种方式:
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作为一个缺省的空的选项。这样可以完成加入不需要选取任何选项的场景。 

posted on 2011-12-19 12:03 PointNet 阅读(319) 评论(1) 编辑 收藏
评论:
  • #1楼[楼主]  PointNet       Posted @ 2011-12-20 13:12
    Try this:

    public class Person {
    public int Id { get; set; }
    public string Name { get; set; }
    }
    And then:

    var list = new[] {
    new Person { Id = 1, Name = "Name1" },
    new Person { Id = 2, Name = "Name2" },
    new Person { Id = 3, Name = "Name3" }
    };

    var selectList = new SelectList(list, "Id", "Name", 2);
    ViewData["People"] = selectList;

    Html.DropDownList("PeopleClass", (SelectList)ViewData["People"])
    With MVC RC2, I get:

    <select id="PeopleClass" name="PeopleClass">
    <option value="1">Name1</option>
    <option selected="selected" value="2">Name2</option>
    <option value="3">Name3</option>
    </select>
     回复 引用 查看   

 
Copyright © PointNet Powered by: 博客园 模板提供:沪江博客