user3200634
user3200634

Reputation: 5

how to pass the value of dropdownlist to controller in asp.net when on-change event triggers

This is my script in my view when dropdownlist wth ID = #APSupplierGroupID triggers on-change event and give the selected value to var $APSupplierGroupID.

<script><script>
$(function () {
    $('#APSupplierGroupID').change(function () {
        var $APSupplierGroupID = $("#APSupplierGroupID option:selected").val();
        $('#APSupplier1').html($APSupplierGroupID);

    });
});    

This is my controller:

public class AcpTnVceController : ControllerBase<APTransactionHeaderEntity, APTransactionHeader>
{
    public AcpTnVceController()
    {

    }

    public ActionResult Event()
    {
        string supplierGroupID = "";

        supplierGroupID = $APSupplierGroupID; //HERE! how can i pass from variable from script $APSupplierGroupID  its value to supplierGroupID from controller?
        return View();
    }

}

How can I pass the value of var $APSupplierGroupID into the controller?

Please help! All responses will be appreciated. Thanks!

Upvotes: 0

Views: 751

Answers (1)

user3559349
user3559349

Reputation:

Your method needs a parameter to accept the posted value

public ActionResult Event(int ID)
{
    // ID will contain the value of the selected option
}

and the script can be

var url = '@Url.Action("Event", "AcpTnVce")';
$('#APSupplierGroupID').change(function () {
    $.post(url, { id: $(this).val() }, function(response) {
        // do something with the returned data
        // $('#APSupplier1').html(response); ??
    });
});

or replace $.post() with $.get() depending on your needs, or for more control, use $.ajax()

Upvotes: 2

Related Questions