Asp net MVC 5 and listbox with linq

After several days of reading on how to fill a listbox with linq i am completely lost on what I have to do to make this work. I know I have to have a viewmodel or something like that but since i cannot reference 2 models in the same view I don t know what to do.

this is the model I have

public class ABC
{
    public int a { get; set; }
    public int b { get; set; }
    public int c { get; set; }
    public string d { get; set; }
    public string Type { get; set; }
    public int ID { get; set; }
 
    public class  ABCDBContext : DbContext
    {
        public DbSet<ABC> ABCs { get; set; }
    }
}
 
public class ABCModel
{
    public string type{ set; get; }
    public List<ABC> ABCs { set; get; }
}

The controller i know i am missing a lot of things but I don´t know how to fill the list with that linq query nor how to call this controller from the view without using beginform (I will have more than 1 listbox in this form ) 

public ActionResult GetStuff()
    {
 
       var Types = from m in db.ABCs
                   select m.Type.Distinct();
 
        return View(Types);
    }

  The controller i know i am missing a lot of things but I don´t know how to fill the list with that linq query nor how to call this controller from the view without using beginform (I will have more than 1 listbox in this form ) 

public ActionResult GetStuff()
    {
 
       var Types = from m in db.ABCs
                   select m.Type.Distinct();
 
        return View(Types);
    }

 And then on my view i am required to have @model IEnumerable so i can show on that page everything that the table abc has. 

So, can I create a viewModel that fills the lsitbox (I will need to fill at least 6 with the parameters that abc has so the user can search for abcs with those values, but i suppose that if i can understand how to do 1 i can do it for 6) with what I want and that allows me to show the entries that abc has? 

and this is the view ( i know a lot of things are wrong with it but its just to give an example) 

@using (Html.BeginForm("getSelectedValues", "ABC", FormMethod.Get))
{
    Type:@Html.DropDownList()
 
    a:@Html.DropDownList()
 
    b:@Html.DropDownList()
 
    //and so on
 
    <input type="submit" value="Filter" />
}

 Pardon for my ignorance for any aspect i missed but i am new to asp net mvc 5, also if anyone could just guide me or send me a guide on what I have to do i would greatly appreciate it.

Last updated:12/26/2014 6:15:15 AM

1 Answers

Anonymous User
Anonymous User

What I would recommend is you create a ViewModel named MYPAGENAMEViewModel.cs, then in that ViewModel you have all the 'model' / data that you need for the view.cshtml file.... i.e. something like the following: 

public class MYPAGENAMEViewModel
{
    public List<Types> MyTypes { get; set; }
    public List<OtherTypes> MyOtherTypes { get; set; }
}

then in your action. 

public ActionResult GetStuff()
{
   MYPAGENAMEViewModel myViewModel = new MYPAGENAMEViewModel();
 
   myViewModel.MyTypes = from m in db.ABCs
                       select m.Type.Distinct();
 
   myViewModel.MyOtherTypes = from m in db.CBAs
                              select m.Type.Distinct();
 
   return View(myViewModel);
}

then at the top of your view: 

@model MYPAGENAMEViewModel

and then later in the view: 

MyTypes<br />

@Html.DropDownListFor(model => model.MyTypes.TypeId, (SelectList)model.MyTypes.TypeName) 

MyOtherTypes<br />

@Html.DropDownListFor(model => model.MyOtherTypes.TypeId, (SelectList)model.MyOtherTypes.TypeName)

Answer