Getting value of bootstrap-select to controller
Hi,
I'm using ASP.NET MVC4 and i'm trying to get the value from the bootstrap-select to pop-up on server side but i just can't get it to work.
@using (Html.BeginForm("ExportCustomers", "Home")) { @Html.DropDownList("Country", new SelectList(@ViewBag.countries, "country_id", "country_desc"), new { @value = 111, @id = "countrydropdown", @name = "countrydropdown", @class = "selectpicker", @onChange = "GetClubs()" }) }
In controller...
public ActionResult ExportCustomers(FormCollection fc) { System.Diagnostics.Debug.WriteLine("Choosen country is: " + fc["countrydropdown"]);
-The result is simply "Choosen country is: ". ...
All help would be greatly appreciated!
Sorry, but I dont use asp.net, so I couldnt understand your code.
To pick the value of the bootstrap-select in js, just use $('select').val() after select one option.
Is the function GetClubs been fired?
Ok i solved it. I changed my form to look like this instead:
@using (Html.BeginForm("ExportCustomers", "Home", FormMethod.Post, new { country_id = "return GetCountry();" })) { @Html.DropDownList("Country", new SelectList(@ViewBag.countries, "country_id", "country_desc"), new { @value = 111, @id = "countrydropdown", @name = "countrydropdown", @class = "selectpicker", @onChange = "GetClubs()" }) }
and then added a route to my controller like this:
routes.MapRoute( name: "ExportCustomers", url: "ExportCustomers/{country_id}", defaults: new { controller = "Home", action = "ExportCustomers", country_id = UrlParameter.Optional } );
In HOME controller...
public ActionResult ExportCustomers(FormCollection fc, int country_id = 0) { System.Diagnostics.Debug.WriteLine("Choosen country is: " + fc["countrydropdown"]); ..
Beginner mistake from my side since im kind of new to MVC.
what does your GetClub do? I have same issue.. ..I only got the first item selected in my dropdownlist when submit.. .I'm using MVC5
Ok i solved it. I changed my form to look like this instead:
@using (Html.BeginForm("ExportCustomers", "Home", FormMethod.Post, new { country_id = "return GetCountry();" })) { @Html.DropDownList("Country", new SelectList(@ViewBag.countries, "country_id", "country_desc"), new { @value = 111, @id = "countrydropdown", @name = "countrydropdown", @Class = "selectpicker", @onchange = "GetClubs()" }) }
and then added a route to my controller like this:
routes.MapRoute( name: "ExportCustomers", url: "ExportCustomers/{country_id}", defaults: new { controller = "Home", action = "ExportCustomers", country_id = UrlParameter.Optional } );
In HOME controller...
public ActionResult ExportCustomers(FormCollection fc, int country_id = 0) { System.Diagnostics.Debug.WriteLine("Choosen country is: " + fc["countrydropdown"]); ..
Beginner mistake from my side since im kind of new to MVC.
This really saved my day! bless you!!