asp.net mvc下拉问题
我在发布后收到错误,当我在 mvc 网站中使用下拉菜单时,代码如下
ViewData["JobSite_JobCategories1"] = new SelectList(context.JobSite_JobCategories, "Id", "Category", null);
<%= Html.DropDownList("JobCategory", ((IEnumerable<SelectListItem>)ViewData["JobSite_JobCategories1"]))%>
<%= Html.ValidationMessageFor(model => model.JobCategory) %>
验证不起作用,并且在我捕获错误后,我再次使用选择列表填充 viewdata["JobSite_JobCategories1"] 但仍然如此给出错误
不存在具有键“JobCategory”的“IEnumerable”类型的 ViewData 项。
请任何人建议我解决这个问题,任何文章、示例、链接。
i am getting error after post, when i am using dropdown in my mvc website, the code is as follows
ViewData["JobSite_JobCategories1"] = new SelectList(context.JobSite_JobCategories, "Id", "Category", null);
<%= Html.DropDownList("JobCategory", ((IEnumerable<SelectListItem>)ViewData["JobSite_JobCategories1"]))%>
<%= Html.ValidationMessageFor(model => model.JobCategory) %>
The validation is not working, and also after i catch error i fill the viewdata["JobSite_JobCategories1"] with selectlist again but still, it gives error
There is no ViewData item of type 'IEnumerable' that has the key 'JobCategory'.
Please can any one suggest me the solution to this problem, any articles, samples, links.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
一切看起来大部分都是正确的。
您正在此处正确构建
SelectList
:我不确定为什么您不只是将其转换为此处的
SelectList
:您不使用
的任何原因>DropDownListFor
来构建它?例如:我不久前还发布了这个问题,您可能会感兴趣阅读:
在 ASP.NET MVC 2 中实现 DropDownList 的最佳方式?
Everything looks correct mostly.
You are building the
SelectList
properly here:I am not sure why you are not just casting it to what it is here, a
SelectList
:Any reason you are not using
DropDownListFor
to build it? Eg:Also I posted this question a while back that might be of interest for you to read:
Best way of implementing DropDownList in ASP.NET MVC 2?