0

Having a hard time finding answer to this question (Google, StackOverflow and ASP.NET).

// Model
public IEnumerable<SelectListItem> State { get; set; }

// View
@Html.DropDownListFor(m => m.State, ????)

// Controller

using (var dbcontext = MyDbContext()) {

    // ??
}

return View(model);

I'm at a complete loss how to populate a mvc dropdownlistfor using LINQ from a DBContext. I understand the DBContext thing and have one setup. I'm not clear on how to construct a linq query and then pass that result (LIST?) to the DropDownListFor helper? Using MS SQL and I have a States table (id, name).

2

2 Answers 2

4

Firstly, in you model, include properties for what you want to bind to and the list of selections

public class MyViewModel
{
  public string StateID { get; set; }
  .... // other properties
  public SelectList StateList { get; set; }
}

In the controller

// Initialise view model
MyViewModel model = new MyViewModel();
// Select the data to display (lots of ways to do this)
var states= from s in dbcontext.States
                 where s.SomeProperty == someValue // if you want to filer the results
                 select s
// Assign select list (this sets the value to the ID property and the display text to the Name property)
model.StateList = new SelectList(states, "ID", "Name");
// and if you want to set a default
model.StateID = "NY";
return View(model);

and in the view

@Html.DropDownListFor(m => m.StateID, Model.StateList)

this will display the full name of the states in the <select> and bind the states ID value to the property StateID

Sign up to request clarification or add additional context in comments.

10 Comments

I saw this exact answer in another SO post and found it a bit confusing. In your model you have CategoryID and CategoryList. How do these properties relate to my State property? Do I need to change my State property from a IEnum to a SelectList? I think I need to see an example based on what I've posted for it to click.
If was just giving an example because I know nothing of your model. Is State typeof string and you want to select from a list of states, or is State a complex property with say properties int ID and string Name?
State has an ID value which is the two letter abbrv for the state and Name is (naturally) the full name of the state. Both are strings.
I didn't realize I had to define two properties.
@Vahx, Not clear what your asking. The property StateID can be a value type such as int Everything sent to and from the browser is text, but the DefaultModelBinder will convert it to the type
|
0
//dbcontext

using (var dbcontext = MyDbContext()) 
{
Dictionary<string, string> states = dbcontext .States.ToDictionary(t => t.StateID.ToString(), t => t.StateName.ToString());

}

// in controller attach this dictionary to ViewBag:

ViewBag.States=states;

// View

 @Html.DropDownListFor(m => m.State,  new SelectList(ViewBag.States, "Key", "Value"))

2 Comments

There seems to be many ways to do this and its starting to click. Although your example doesn't seem to consider the model? Or I'm I misreading it?
Well you can create property in model in your model Dictionary<string, string> states {get;set;} //and in controller: model.states=states; // return from dbcontext // in view replace ViewBag.States with model.states.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.