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 @   black娃  阅读(2704)  评论(0编辑  收藏  举报
编辑推荐:
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
点击右上角即可分享
微信分享提示