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?
Share
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:
Foreach(var m in Model.Methods) {
items.Add(new SelectListItem{Value=@m.Id.toString() , Text=@m.MethodName})
}
Then you could use html helpers that can help you bind model
In your controller that gets the post request use your model with task and methods as a parameter then just validate and savechanges