The DropDownList helper used to create an HTML select list requires a IEnumerable , either explicitly or implicitly. That is, you can pass the IEnumerable explicitly to the DropDownList helper or you can add the IEnumerable to the ViewBag using the same name for the SelectListItem as the model property.
可以传入明确的IEnumerable<SelectListItem>,也可以通过ViewBag或者ViewData隐式地传入,前提是需要相同的名称,比如:
ViewBag.GenreId或者ViewData["GenreId"]。官方示例:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | public ActionResult SelectCategory() { List<SelectListItem> items = new List<SelectListItem>(); items.Add( new SelectListItem { Text = "Action" , Value = "0" }); items.Add( new SelectListItem { Text = "Drama" , Value = "1" }); items.Add( new SelectListItem { Text = "Comedy" , Value = "2" , Selected = true }); items.Add( new SelectListItem { Text = "Science Fiction" , Value = "3" }); ViewBag.<strong>MovieType</strong> = items; ViewData[ "HourList" ] =items ; return View(); } |
视图:
@Html.DropDownList("MovieType")
以上来自:https://q.cnblogs.com/q/78044/
不相同名称的处理:
@Html.Kendo().DropDownListFor(l => l.lb).OptionLabel("Select...").DataTextField("Text").DataValueField("Value").BindTo(ViewData["HourList"] as List<SelectListItem>))
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· 三行代码完成国际化适配,妙~啊~
· .NET Core 中如何实现缓存的预热?
· 如何调用 DeepSeek 的自然语言处理 API 接口并集成到在线客服系统
2018-08-20 DDD~大话目录
2018-08-20 基于DDD的.NET开发框架-DDD经典分层