问题
How to get dropdownList selected Item from CSHTML page.
<div class="editor-field">
@Html.DropDownList("ROUTE_GROUP_ID", String.Empty)
@Html.ValidationMessageFor(model => model.ROUTE_GROUP_ID)
</div>
回答1:
If you want its value in jquery you can do like this
$('#ROUTE_GROUP_ID').val();
or if you want its value in controller you can access it from
Request.Form["ROUTE_GROUP_ID"]
or if your controller have a formcollection object then access the value like
formcollectionobj["ROUTE_GROUP_ID"]
回答2:
From your example, i don't see how you would get any selected value, since you haven't defined the SelectList from which your DropDownList will get it's values.
I'd suggest you to create a ViewModel, fill a SelectItemList with your RouteGroup, passing it's ID as value. Like this:
public class RouteGroupViewModel
{
public string SelectedRouteGroup { get; set; }
public List<SelectListItem> RouteGroup { get; set; }
public void FillRouteGroup()
{
//Fill your SelectList with your RouteGroup values
}
}
On your view:
@Html.DropDownListFor(item => item.SelectedRouteGroup, new SelectList(Model.RouteGroup, "Value", "Text"))
And on your Controller:
public ActionResult RouteGroup(RouteGroupViewModel rgVM)
{
//To Do your controller operations
}
With that you can get the DropDownList selected value.
来源:https://stackoverflow.com/questions/7513149/how-to-get-dropdownlist-selected-value