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' к 'System.Collections.Generic.List'
Похоже, я использую неправильный шаблон. Как мне выдать System.Entity.Infrastructure.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 )
}
И я получаю следующее:
Ссылка на объект не установлена на 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.