asp.net mvc框架之下拉框(DropDownList)

asp.net mvc框架中下拉框的几种使用方式

方法一:

        public ActionResult Index()
        {
            List<Person> list = new List<Person>();
            Person p1 = new Person() { id = 1, name = "张三" };
            Person p2 = new Person() { id = 2, name = "李四" };
            Person p3 = new Person() { id = 3, name = "王五" };
            Person p4 = new Person() { id = 4, name = "仁六" };
            list.Add(p1);
            list.Add(p2);
            list.Add(p3);
            list.Add(p4);
            return View(list);
        }        
@using WebApplication2.Models
@model List<Person>
@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Index</title>
</head>
<body>
    <div> 
        <select>
            @foreach (Person p in Model)
            {

                <option selected="@(p.id==3)">@p.name</option>
            }
        </select>
    </div>
</body>
</html>

 

方法二:

public ActionResult SelectItemTest()
        {
            List<Person> list = new List<Person>();
            Person p1 = new Person() { id = 1, name = "张三" };
            Person p2 = new Person() { id = 2, name = "李四" };
            Person p3 = new Person() { id = 3, name = "王五" };
            Person p4 = new Person() { id = 4, name = "仁六" };
            list.Add(p1);
            list.Add(p2);
            list.Add(p3);
            list.Add(p4);

            List<SelectListItem> sliList = new List<SelectListItem>();


            foreach (Person item in list)
            {
                SelectListItem ListItem = new SelectListItem();
                ListItem.Selected = (item.id == 4);
                ListItem.Text = item.name;
                ListItem.Value = item.id.ToString();
                sliList.Add(ListItem);
            }
            return View(sliList);
        }
@model IEnumerable<SelectListItem>


<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>SelectItemTest</title>
</head>
<body>
    <div> 
        @Html.DropDownList("pid", Model);
    </div>
</body>
</html>

重点:

1.导入@model IEnumerable<SelectListItem>

2.使用@Html.DropDownList("pid", Model);

bool Selected:是否选中状态,也就是是否生成selected="selected"属性;

string Text:显示的值,也就是<option>里面显示的值

string Value:生成的value属性,是string类型

如果在DropDownList("pid",Model,new {id="sid",name="abc"}) 第一个参数id,第二个参数传递的值,第三个参数是可以自定义下拉框里面的属性,比如:info="aaa" 最终就显示到了这个下拉框里面。注意:如果使用class="",要在class前面添加@符号。

 

方法三:

使用SelectList可以帮助我们自动遍历集合

public ActionResult SelectItemTest3()
        {
            List<Person> list = new List<Person>();
            Person p1 = new Person() { id = 1, name = "张三" };
            Person p2 = new Person() { id = 2, name = "李四" };
            Person p3 = new Person() { id = 3, name = "王五" };
            Person p4 = new Person() { id = 4, name = "仁六" };
            list.Add(p1);
            list.Add(p2);
            list.Add(p3);
            list.Add(p4);

            SelectList selList = new SelectList(list, "Id", "Name",2);
            ViewBag.aaa = selList;
            return View();
        }
@model IEnumerable<SelectListItem>

@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>SelectItemTest3</title>
</head>
<body>
    <div> 
        @Html.DropDownList("pid",(SelectList)ViewBag.aaa)
    </div>
</body>

 

 

其中:ListBox使用方法和DropDownList方法一样

@Html中还有其他:

TextBox

TextArea

 

posted @ 2019-06-13 13:35  black娃  阅读(2570)  评论(0编辑  收藏  举报