0

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!

  • You'll need a parameter in your Event action, and then to call it from (AJAX?) from javascript using GET or POST – Stuart.Sklinar Jun 19 '15 at 11:07
  • you have to you jquery ajax – dansasu11 Jun 19 '15 at 11:07
  • possible duplicate of [Making a Simple Ajax call to controller in asp.net mvc](http://stackoverflow.com/questions/16186083/making-a-simple-ajax-call-to-controller-in-asp-net-mvc) – Max Jun 19 '15 at 11:07
  • Do you want to do a POST or a GET? –  Jun 19 '15 at 11:08
  • 1
    [Possible duplicate](http://stackoverflow.com/questions/16186083/making-a-simple-ajax-call-to-controller-in-asp-net-mvc) – Max Jun 19 '15 at 11:08
  • I want to get the value from the dropdown everytime the user change selection.. i want that value to pass to the controller.. – user3200634 Jun 21 '15 at 12:26

1 Answers1

2

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()