kkami-sama
kkami-sama

Reputation: 15

how to get selected dropdown id from jquery to controller

I have a script like

$.each(result.Data, function (key, value) {
                if (value.RateName === "")
                    return;
                $('#AllRateList')
                    .append($("<option></option>")
                        .attr("value", value.Id)
                        .text(value.RateName));
            });
            $('select').change(function () {
                selectedRateId = $(this).val()
                console.log(selectedRateId)
            });

when i select an option at localhost i can see the id from log. I want to take this selectedRateId from jquery and use it for update when press the save button. here is the service with customerId

public void UpdateRatePlan(int customerId)
        {

            var updateProductVoices = (from product in productRepository.All()
                                    join productVoice in productVoiceRepository.All()
                                    on product.Id equals productVoice.Id
                                    where product.CustomerId == customerId && product.Status == "A"
                                    select productVoice).ToList();
            

        }

I can add more details if need it. I couldnt figure out.

Upvotes: 0

Views: 312

Answers (1)

iamdlm
iamdlm

Reputation: 1973

Is UpdateRatePlan called from a controller or is that a controller action? If it is you'll need to change it to this:

[HttpGet]
public ActionResult UpdateRatePlan()
{
    // your code
} 

And call it in your client side:

$('select').change(function () {
    selectedRateId = $(this).val();
    $.ajax({
        url: 'UpdateRatePlan',
        data: { customerId: selectedRateId} ,
        success: function(response){ // do something with response },
        error: function(response){ // do something with response }
    });
});

Upvotes: 1

Related Questions