Reputation: 9279
I have an MVC3 C#.Net web site and I have a lookup table "Methods" in SQL server. I want to create a drop down list that populates the list of values from the "Name" column in my "Methods" table. I have an object, "Task", that has a string property "MethodName". I want to attach the selected value from the Drop Down list to this property in the Task object. How do I do this?
Upvotes: 1
Views: 697
Reputation: 1060
Make a model that contains a task and a list of methods. Get all the methods and the task you want from db in your "custom" model. Pass the model into your view
Set in your view in the top @model NameProject.Folder.Modelname
Then add to your view:
List<SelectListItem> items = new List< SelectListItem>();
Foreach(var m in Model.Methods) { items.Add(new SelectListItem{[email protected]() , [email protected]}) }
Then you could use html helpers that can help you bind model
@Html.DropDownListFor(model => Model.Task.Method, items)
In your controller that gets the post request use your model with task and methods as a parameter then just validate and savechanges
Upvotes: 1