bootstrap-select icon indicating copy to clipboard operation
bootstrap-select copied to clipboard

Getting value of bootstrap-select to controller

Open sorenmp3 opened this issue 13 years ago • 4 comments

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!

sorenmp3 avatar Jul 22 '13 15:07 sorenmp3

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?

silviomoreto avatar Jul 22 '13 17:07 silviomoreto

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.

sorenmp3 avatar Jul 23 '13 09:07 sorenmp3

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

sir-bsbaril avatar Apr 18 '16 09:04 sir-bsbaril

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!!

mariumr avatar Jan 26 '21 00:01 mariumr