Passing a list inside a viewmodel

I'm getting an error that says "Cannot implicitly convert type 'System.Collections.Generic.List<[projectName].State>' to 'System.Collections.Generic.List<[projectName].Models.States>'

I would like to pass a list to a view via a viewmodel with other Vendor info.  Here's what I have so far:

I have a "States" model:
 public class States
    {
        public int StateID { get; set; }
        public string Code { get; set; }
        public string Name { get; set; }
    }

Open in new window


Then I have a "Vendor" model:

  public class Vendor
    {
        public string NameOfBusiness { get; set; }
        public string DescriptionOfBusiness { get; set; }
        public string BillingAddress1 { get; set; }
    }

Open in new window


Then I have a "VendorViewModel":
 
public class VendorViewModel
    {
        public Vendor Vendor { get; set; }

        //States for dropdown
        public List<States> States { get; set; }
    }

Open in new window

green_thumb_98Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Bob LearnedCommented:
There is nothing there to explain that error, so can you show me where the model is defined in the view?
0
green_thumb_98Author Commented:
I haven't gotten to the view yet.  I am getting the error in the following controller code when trying to assign StateList to viewModel.States  (sorry I forgot to include this in original post):

  public ActionResult Index()
        {
            //get list of states
            VendorViewModel viewModel = new VendorViewModel();
            

            //List<UserViewModel> objList = UserService.GetUsers();

            List<State> StateList = _db.States.ToList();

            viewModel.States = StateList;

            return View("Vendor", viewModel);
        }

Open in new window

0
Bob LearnedCommented:
It seems like this line is the culprit:

List<State> StateList = _db.States.ToList();

What happens if you use this instead:

var stateList = _db.States.ToList();
0
Become a CompTIA Certified Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

green_thumb_98Author Commented:
It still shows an error when assigning StateList to viewModel.States.

error
0
Ioannis ParaskevopoulosCommented:
You are trying to pass a List<State> which is obviously an entity from your db, to a List<States> which is the class defined by you in your model. Notice the deferense in the final 's' of State vs States.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Bob LearnedCommented:
Oh, man, you have a different State class from the one that is defined from the data context (d'oh).
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.