不存在带有键“Blah”的 ViewData 项。 类型为“IEnumerable”
这个错误消息让我抓狂。
我在 ASP.NET MVC v1.0 中使用 Html.ListBox 和 Html.DropDownList HtmlHelpers 时得到它。 填充列表工作正常 - 我可以查看它们等 - 但是当我使用传递到控制器的 FormCollection 以获取所选值在模型中创建新记录时,会发生此错误。 我已经按照有关此事的其他几篇文章尝试了几种不同的配置和代码安排 - 没有乐趣。
希望有人能告诉我这是否是一个已知问题,以便我可以继续我的生活! 如有必要,我宁愿不必发布代码,但如果需要,可以这样做。
哦,建议的解决方法/解决方案也很好;-)
TIA!
伯纳德.
好的,快速更新,我已经在下面的回复中发布了三条评论,以澄清我在这里所做的事情的详细信息。
我现在有另一个问题要分享 - 我已经尝试了 ScottGu 的替代推荐方法 - 实现 ModelView。 因此,当我这样做时,基本上是相同的场景,但我得到一个“对象引用未设置到对象实例”。 在我收到“没有带有类型为‘IEnumerable’的键‘Blah’的 ViewData 项。”的错误。
我开始认为这可能与 EF 的工作方式有关......
This error message is driving me nuts.
I'm getting it when using Html.ListBox and Html.DropDownList HtmlHelpers with ASP.NET MVC v1.0. Populating the lists works OK - I can view them, etc - but when I go to create a new record in the Model using the FormCollection passed into the Controller to get the selected value, this error occurs. I've tried several different configurations and code arrangements as per several other posts on this matter - no joy.
Hopefully someone can tell me whether this is a known issue or not so as I can move on with my life! Would prefer not to have to post code if necessary, but can do if requested.
Oh, and a suggested workaround/solution would be nice too ;-)
TIA!
Bernard.
Ok, quick update, I've posted three comments to responses below clearing up details as to what I'm up to here.
I now have another issue to share - I've tried ScottGu's alternative recommended approach - implementing a ModelView. So, when I do this it's basically the same scenario, but I get an "Object reference not set to an instance of an object." error at the same place that I was getting "There is no ViewData item with the key 'Blah' of type 'IEnumerable'.".
I'm starting to thing that this is perhaps something to do with the way the EF works...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
好吧,所以我显然是一个 EF/MVC 菜鸟...
虽然这个问题是通过我实际实现适当的异常管理解决的...异常消息(当我真正开始捕获它时)是“两个对象之间的关系不能被定义,因为它们附加到不同的 ObjectContext 对象。”。 因此,我现在对所有数据访问使用相同的 ObjectContext,一切都回到正轨。
谢谢你的关心,下次我会问一个更聪明的问题——保证!
伯纳德.
Ok, sooo I'm clearly an EF/MVC noob...
Although the issue was solved by my actually implementing proper exception management...the exception message (when I actually started catching it) was "The relationship between the two objects cannot be defined because they are attached to different ObjectContext objects.". So I'm using the same ObjectContext for all data access now, and things are back on track.
Thanks for caring, I'll have a smarter question next time - promise!
Bernard.
不知道你是如何编码的,人们将很难识别有问题的区域。 阅读此论坛帖子 - 再往下看也有与您类似的错误。
http://forums.asp.net/p/1320426/2623986.aspx
Not knowing how you are coding this will be hard for people to identify the problematic area. Have a read of this forum thread - further down has a similar error to you.
http://forums.asp.net/p/1320426/2623986.aspx
您可以在进行验证的相应 post 方法中再次绑定选择列表。
例如,您在控制器的创建视图中出现错误,您可以在创建帖子方法中再次绑定下拉列表。
You can again bind your select list in the according post method where validation occurs.
For example you have an error inside the create view of the controller than you can again bind dropdown list in create post method.