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
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 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语句:使用策略模式优化代码结构