在 viewbag 中传递查询结果
这看起来应该很容易,但我尝试了三四种方法(但没有成功)。
我只是想将查询结果放入视图袋中并显示它。
我尝试将模型对象列表放入 ViewBag:
var mesg = from MSG in lemondb.Messages
where MSG.msg == Membership.GetUser().ToString()
select MSG;
ViewBag.messages = MSG;
然后我尝试将其吐出在 .cshtml:
var message = (List<LemonTrader.Models.Message>)ViewBag.messages; // <--- fails here because it is a string
foreach ( var MSG in message )
{
@Html.Label(MSG.msg)<br />
}
但它说:
无法转换类型 '系统.数据.实体.基础设施.DbQuery' 到 'System.Collections.Generic.List'
所以看来我使用了错误的模板。如何吐出 System.Entity.Infrastruct.DbQuery?
我还尝试将结果作为字符串列表通过 Viewbag 传递。 (这是一个更糟糕的方法吗?)
var mesg = from MSG in lemondb.Messages
where MSG.msg == Membership.GetUser().ToString()
select MSG.msg;
ViewBag.messages = mesg;
并将其作为字符串列表吐出:
foreach (var atext in ViewBag.messages as List<string>) { // gets hung up on foreach here (why???)
@Html.Label( atext )
}
我得到了这个:
对象引用未设置为 对象的实例。
它指向“foreach”关键字。
这是没有消息的意思吗?或者什么?
我希望有一个教程展示如何将查询结果放入 ViewBag 以及如何将它们取出!我见过返回 object.ToList() 的教程,而不考虑任何类型的“where”机制,但没有示例来提取一些相关条目并显示它们。
This seems like it should be so easy, but I've tried three or four ways to do it (but to no avail).
I'm just trying to put a query result in a viewbag and display it.
I've tried putting a model object list in a ViewBag:
var mesg = from MSG in lemondb.Messages
where MSG.msg == Membership.GetUser().ToString()
select MSG;
ViewBag.messages = MSG;
And then I try to spit it out in a .cshtml:
var message = (List<LemonTrader.Models.Message>)ViewBag.messages; // <--- fails here because it is a string
foreach ( var MSG in message )
{
@Html.Label(MSG.msg)<br />
}
But it says:
Cannot convert type
'System.Data.Entity.Infrastructure.DbQuery'
to
'System.Collections.Generic.List'
So it seems I'm using using the wrong template. How do I spit out a System.Entity.Infrastructure.DbQuery?
I've also tried passing the results through the Viewbag as a list of strings. (Is that a worse way to do it?)
var mesg = from MSG in lemondb.Messages
where MSG.msg == Membership.GetUser().ToString()
select MSG.msg;
ViewBag.messages = mesg;
And spitting it out as a string list:
foreach (var atext in ViewBag.messages as List<string>) { // gets hung up on foreach here (why???)
@Html.Label( atext )
}
And I get this:
Object reference not set to an
instance of an object.
And it points at the "foreach" keyword.
Does that mean there were no messages? Or what?
I wish there was a tutorial showing how to put queryresults in a ViewBag and how to get them out! I've seen tutorials that return an object.ToList() without respect to any kind of "where" mechanism, but no examples to pull out a few, relevant entries and display them.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
另请尝试
,System.Data.Entity.Infrastruct.DbQuery 实现 IEnumerable ( http://msdn.microsoft.com/en-us/library/system.data.entity.infrastruct.dbquery(v=vs.103).aspx )所以这也应该有效:
Try
Also, System.Data.Entity.Infrastructure.DbQuery implements IEnumerable ( http://msdn.microsoft.com/en-us/library/system.data.entity.infrastructure.dbquery(v=vs.103).aspx ) so this should also work: