Click here to Skip to main content
15,867,756 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello House,

i came from asp.net drag and drop background, i want to be able to get the selected value, selected index etc of a drop downlist control in the controller code in mvc5. here is my view code:

HTML
@Html.DropDownList("Role",new List<SelectListItem>
                        {
                             new SelectListItem{Text="Select Role", Value="0",Selected =true } ,
                            new SelectListItem{Text="Student", Value="1" } ,
                            new SelectListItem{Text="Lecturer", Value="2"  }
                           
                        }, "") 


Any assistance will be appreciated.
Posted

1 solution

first you should define view model and than bind your control to model

C#
@model ViewModel.someViewModel
 @Html.DropDownListFor(model => model.ID, new SelectList(ViewBag.Roll, "ID", "Name"))

then in your controller action method should receive  data

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Update(viewModel model)
{
//do some thing
}
 
Share this answer
 
Comments
Uwakpeter 13-Jan-15 10:59am    
i can do this, what i wanted was to be able to get the selected value, in normal web form i could do :string ddValue =dropDownlist1.SelectedValue or int ddValue =dropDownlist1.SelectedIndex
MohsenSaleh 13-Jan-15 13:29pm    
it seem doesn't possible directly
MohsenSaleh 13-Jan-15 13:28pm    
it may helping you
http://www.experts-exchange.com/Programming/Languages/C_Sharp/Q_28131841.html

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900