0

I've googled this and tried so many different variations. I think the issue I'm having is there are so many ways to do this, and I'm not seeing the wood for the trees.

I have a list of equipment in a database. Each row is assigned to a company. There could be 10 pieces of equipment for company "Contoso Ltd", 23 pieces of equipment for "Geldof Industries" etc.

I have a scaffolded view using entity framework to display all the equipment for the entire database. This works fine.

What I want to do is have a dropdown list on the view, which populates with a list of distinct companies in the table and selecting a company then filters the results in the view.

At the moment my controller has this:

    var serverList = from s in db.tbl_equipment
                     where (s.Company == "Contoso Ltd") && (s.Calc_Contract_Status == true)
                     where (s.Equip_type == "PC") || (s.Equip_type == "Laptop") || (s.Equip_type == "Mac") || (s.Equip_type == "Tablet") || (s.Equip_type.Contains("Server"))
                     select s;

    return View(serverList.ToList());

As I understand it, I would populate a dropdown list based on a LINQ query, and use a viewbag to send that to the view then use a Dropdownlistfor html helper to create the dropdown. I'd assume I'd then need to capture the post request in someway back in the controller.

Every single site I have found for generating a dropdownlist in Razor and ASP.NET Entity Framework is different. Any help would be much appreciated!

Rahul Singh
  • 21,585
  • 6
  • 41
  • 56
Trinitrotoluene
  • 1,388
  • 5
  • 20
  • 39
  • Show your model (it needs a property to bind the selected option value to) –  Nov 11 '14 at 11:27
  • The model is quite large, the property would be tbl_equipment.Company which is a string. – Trinitrotoluene Nov 11 '14 at 11:31
  • The answers [here](http://stackoverflow.com/questions/26800310/what-is-this-line-of-code-viewbag-roleid-new-selectlistrolemanager-roles-id/26800527#26800527) and [here](http://stackoverflow.com/questions/25676914/i-cant-get-a-dropdownlist-to-populate-from-table-ef-and-mvc4/25676985#25676985) might help –  Nov 11 '14 at 11:40

1 Answers1

0

Lets say you want to populate a drop down list comprising of list of servers.

Create a view model (you can do it with viewbag,viewdata too, but it is much better to do it this way)

ViewModel

public class ServerViewModel
{
    // Display Attribute will appear in the Html.LabelFor
    [Display(Name = "Server")]
    public int SelectedserverId { get; set; }
    public IEnumerable<SelectListItem> ServerList { get; set; }
}

Inside the controller create a method to get your Sever list and transform it into the form that will be presented in the view.

Controller:

private IEnumerable<SelectListItem> GetAllServers()
{


var serverList = from s in db.tbl_equipment
                     where (s.Company == "Contoso Ltd") && (s.Calc_Contract_Status == true)
                     where (s.Equip_type == "PC") || (s.Equip_type == "Laptop") || (s.Equip_type == "Mac") || (s.Equip_type == "Tablet") || (s.Equip_type.Contains("Server"))
                     select s;

    var servers = serverList 
                .Select(x =>
                        new SelectListItem
                            {
                                Value = x.serverId.ToString(), //assuming you have this two property           in your table
                                Text = x.severName
                            });

    return new SelectList(servers , "Value", "Text");
}

public ActionResult GetServerList()
{
    var model = new ServerViewModel
                    {
                        ServerList = GetAllServers()
                    };
    return View(model);
}

Now that the viewmodel is created the presentation logic is simplified

View:

@model ServerViewModel

@Html.LabelFor(m => m.SelectedserverId )
@Html.DropDownListFor(m => m.SelectedserverId , Model.ServerList )

And your list is displayed. Hope this helps....

ankur
  • 4,565
  • 14
  • 64
  • 100
  • Hi, thanks - this looks like the proper way to go. I'd assume my ViewModel would be based on the company though. The Value and Text for the dropdown list would both have to company name, which is a string. How would I factor that in? – Trinitrotoluene Nov 11 '14 at 11:51
  • yes your view model will be based on company. if you go over this part of the code.... var servers = serverList .Select(x => new SelectListItem { Value = x.serverId.ToString(), //assuming you have this two property in your table Text = x.severName }); You can use bot value and text as x.CompanyName and see how that works for you. Also check the generated markup of the browser to see how the drop down list is being generaed – ankur Nov 11 '14 at 11:56
  • Thanks for that. In my view I already have an @model and it says only one model statement is allowed in a file? – Trinitrotoluene Nov 11 '14 at 12:00
  • ok so create wrapper for that in lay man terns create a class that will have two properties . Each of this will be a reference to your respective models so that you will have only one model. user this link http://stackoverflow.com/questions/5550627/two-models-in-one-view-in-asp-mvc-3 – ankur Nov 11 '14 at 12:02
  • I'm afraid you've lost me at that point. It's difficult for me to conceptualise in my head without seeing code - thanks for your help so far – Trinitrotoluene Nov 11 '14 at 12:04