asp.net-mvc – 如何使用ViewBag创建一个下拉列表?
发布时间:2020-12-15 23:16:41 所属栏目:asp.Net 来源:网络整理
导读:控制器: public ActionResult Filter(){ ViewBag.Accounts = BusinessLayer.AccountManager.Instance.getUserAccounts(HttpContext.User.Identity.Name); return View();} 视图: tdAccount: /tdtd@Html.DropDownListFor("accountid",new SelectList(ViewBa
控制器:
public ActionResult Filter() { ViewBag.Accounts = BusinessLayer.AccountManager.Instance.getUserAccounts(HttpContext.User.Identity.Name); return View(); } 视图: <td>Account: </td> <td>@Html.DropDownListFor("accountid",new SelectList(ViewBag.Accounts,"AccountID","AccountName"))</td> ViewBag.Accounts包含具有AccountID,AccountName和其他属性的Account对象. 在代码中我做错了什么? 解决方法
你不能使用Helper @ Html.DropdownListFor,因为第一个参数是不正确的改变你的帮手:
@Html.DropDownList("accountid","AccountName")) @ Html.DropDownListFor在第一个参数中接收所有重载中的lambda表达式,并用于创建强类型的下拉列表 Here’s the documentation 如果您的查看它强大类型为某些模型,您可以使用帮助器更改您的代码,以创建一个强类型的下拉列表,像 @Html.DropDownListFor(x => x.accountId,"AccountName")) (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- 在asp.net vnext上使用bearer token身份验证刷新令牌
- asp.net-mvc – 如何阻止ASP.Net MVC Html.ActionLink使用现
- asp.net – 为什么在web.config中保存动态数据是个坏主意?
- asp.net – IIS 6.0和.Net 4的Default.aspx?
- asp.net-mvc – 如何避免打开重定向漏洞并成功登录安全地重
- 动态背景图像(ASP.NET)
- 单元测试 – 如何在MVC 6中存根/模拟AuthenticationManager
- asp.net – 通过CSS重置HTML元素的高度
- asp.net-core – 在ASP.NET 5 MVC6中使用什么而不是WebView
- asp.net – 使用Visual Studio 2017在.NET Core解决方案上没
推荐文章
站长推荐
- asp.net-mvc – 登录MVC3中的Razor视图
- asp.net-mvc – 在本地PC上,IIS和浏览器使用大结
- razor – 从ASP.net Core 2中的视图访问自定义Ap
- asp.net-mvc – MVC发布IPagedList
- 如何在ASP.NET MVC模型中为POST保存选定的DropDo
- 在ASP.NET Core应用中如何设置和获取与执行环境相
- 如何在asp.net上模拟同时的web请求?
- asp.net – 在Global.asax中使用Trace
- asp.net-mvc-4 – TempData不适用于MVC4中的第二
- asp.net-mvc – 如何只允许mvc4 razor中的文本框
热点阅读