How to get Dropdownlist selected value

送分小仙女□ 提交于 2020-01-07 01:30:07

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!