Link to home
Start Free TrialLog in
Avatar of Charles Baldo
Charles BaldoFlag for United States of America

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.AdvertisementImageLocation,
                                     }).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.Generic.List<AnonymousType#1>' to 'System.Collections.Generic.List<Info.Models.Advertisement>'      C:xxxxx\Info\Controllers\HomeController.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??
ASKER CERTIFIED SOLUTION
Avatar of kaufmed
kaufmed
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Charles Baldo

ASKER

Perfect Thank You