I am trying to pass data from controller to view. I have searched the web but could not find a solution.
if I do this it works:
Controller:
var yyy = (from a in Connection.Db.Authorities select a) ;
ViewBag.data = yyy;
View:
@foreach(var item in ViewBag.data)
{
@item.Value
}
But the following code does not work:
Controller:
var yyy = (from a in Connection.Db.Authorities select new {Value = a.Value, TypeCode = a.TypeCode, Return = Calculate(a.Return)}) ;
ViewBag.data = yyy;
View:
@foreach(var item in ViewBag.data)
{
@item.Value
}
It gives "item does not contain a definition for Value" for the view file.
Any help would be great.
Thank you.
-edited: updated the second controller linq query. and corrected the first controller linq query.
use sth like this
ViewBag.qualification = new SelectList(db.Lookups.Where(x => x.lookup_type == "Qualification"), "lookup_content", "lookup_content");
It's because You already select
Value
andValue
has no such property asValue
. You should change in controller:var yyy = (from a in Connection.Db.Authorities select a.Value);
toOR change the view to
//////////////////////////////////////////////// EDITS ////////////////////////////////////////////////
Than You should not use anonymous object. You should create
ViewModelClass
. For Example:And change your controller:
and in your view you will be able to use:
Also, I have a question to You. Why do You use
ViewBag
to pass data from controller to view? Why don't You use Model to pass these data to view according to MVC pattern?//////////////////////////////////////////////// MORE EDITS ////////////////////////////////////////////////
To send more than one query result You can create more complex model. For example:
And change the controller:
and in view you can use: