Charles Baldo
asked on
Need to pass 2 lists to MVC view
New to MVC and LINQ
I need to pass two lists to an mvc5 razor view. Both lists originate from Models retrieve from and Entity Frameworks model (edmx)
I want to create a LINQexpression to select only specific records
Here is the model (class) that I want to pass in
-------------------------- --------
public class MyViewModel
{
public List<Article> ArticleList { get; set; }
public List<Advertisement> AdvertisementList { get; set; }
}
-------------------------- ----------
Here is the controller code
-------------------------- ---------
var ArticleList = db.Articles.ToList();
var AdvertisementList =
(from s in db.Advertisements
where s.AdvertisementID < 3
select new
{
s.AdvertisementID,
s.AdvertisementText,
s.AdvertisementAlt,
s.AdvertisementImageLocati on,
}).ToList();
MyViewModel mvm = new MyViewModel();
mvm.ArticleList = ArticleList;
mvm.AdvertisementList = AdvertisementList; <------------------------ Error Here
return View(mvm);
The error I get is Error 1 Cannot implicitly convert type 'System.Collections.Generi c.List<Ano nymousType #1>' to 'System.Collections.Generi c.List<Inf o.Models.A dvertiseme nt>' C:xxxxx\Info\Controllers\H omeControl ler.cs 37 37 Info
I get it that AdvertisementList was not a type of Advertisement it was an anonymous type declared from var AdvertisementList = .....
I also tried this
var AdvertisementList = db.Advertisements.Where(s => s.AdvertisementID < 3);
My question is how do I use LINQ or whatever to get my SELECTED list of Advertisements from the controller to the view??
I need to pass two lists to an mvc5 razor view. Both lists originate from Models retrieve from and Entity Frameworks model (edmx)
I want to create a LINQexpression to select only specific records
Here is the model (class) that I want to pass in
--------------------------
public class MyViewModel
{
public List<Article> ArticleList { get; set; }
public List<Advertisement> AdvertisementList { get; set; }
}
--------------------------
Here is the controller code
--------------------------
var ArticleList = db.Articles.ToList();
var AdvertisementList =
(from s in db.Advertisements
where s.AdvertisementID < 3
select new
{
s.AdvertisementID,
s.AdvertisementText,
s.AdvertisementAlt,
s.AdvertisementImageLocati
}).ToList();
MyViewModel mvm = new MyViewModel();
mvm.ArticleList = ArticleList;
mvm.AdvertisementList = AdvertisementList; <------------------------ Error Here
return View(mvm);
The error I get is Error 1 Cannot implicitly convert type 'System.Collections.Generi
I get it that AdvertisementList was not a type of Advertisement it was an anonymous type declared from var AdvertisementList = .....
I also tried this
var AdvertisementList = db.Advertisements.Where(s => s.AdvertisementID < 3);
My question is how do I use LINQ or whatever to get my SELECTED list of Advertisements from the controller to the view??
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER