---
title: "Asp net MVC 5 and listbox with linq"  
description: "Asp net MVC 5 and listbox with linq"  
author: "Anonymous User"  
published: 2014-12-26  
updated: 2014-12-26  
canonical: https://www.mindstick.com/forum/12814/asp-net-mvc-5-and-listbox-with-linq  
category: ".net"  
tags: ["linq", "mvc5", "listbox"]  
reading_time: 3 minutes  

---

# Asp net MVC 5 and listbox with linq

After several days of [reading](https://www.mindstick.com/articles/126273/books-commonly-found-on-college-reading-lists) on how to fill a [listbox](https://www.mindstick.com/articles/626/listbox-events-in-c-dot-net) with linq i am completely lost on what I have to do to make this work. I know I have to have a [viewmodel](https://www.mindstick.com/forum/157215/what-is-viewmodel-in-mvc) or something like that but since i cannot [reference](https://www.mindstick.com/forum/774/reference-what-does-this-error-mean-in-php) 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](https://www.mindstick.com/blog/273/passing-values-from-controller-to-view-in-asp-dot-net-mvc) i know i am [missing](https://answers.mindstick.com/qa/52138/international-missing-children-s-day-2019-was-observed-on) a lot of things but I don´t know how to fill the list with that [linq query](https://www.mindstick.com/forum/33908/how-to-select-and-retrieve-data-using-linq-query-in-entity-framework) 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](https://www.mindstick.com/forum/160269/what-is-required-data-annotation-how-does-it-affect-model-validation) 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](https://www.mindstick.com/articles/87/passing-parameters-in-c-sharp) 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](https://www.mindstick.com/forum/34120/what-is-the-use-glimpse-in-asp-dot-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.

## Replies

### Reply by 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)


---

Original Source: https://www.mindstick.com/forum/12814/asp-net-mvc-5-and-listbox-with-linq

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
