John S
John S

Reputation: 8351

asp.net mvc submit form using jquery mobile with parameter

I have a mobile form with a single dropdownlist of locations. When I select a location from the DDL I want the form to submit to an action.

My Razor:

@Html.DropDownListFor(a => a.SelectedLocationID
, Model.AllLocation
, "Select your location")

My Jquery:

  $("#SelectedLocationID").on("change", function () {
        //var $id = $(this).val();
        $("form").submit();
        //$("form").submit({ "id": $id }, test);
    })

The issue is that the action requires a variable of "id" but the selected location variable is SelectedLocationID. How can I change that variable name dynamically?

Upvotes: 1

Views: 385

Answers (1)

Davor Zlotrg
Davor Zlotrg

Reputation: 6050

You can not use DropDownListFor in this case because it is strongly typed and the name is generated based on the lambda expression you are passing as the argument.

You could change DropDownListFor to DropDownList instead, and pass in the name:

@Html.DropDownList("id", new SelectList(Model.AllLocation), "Select your location");

And you will get the "id" in your action method.

Upvotes: 1

Related Questions