Pass a parameter to a controller using jquery ajax

前端 未结 3 1538
春和景丽
春和景丽 2020-12-01 16:18

I have created a view and a controller, the controller I am wanting to return some search results. I am calling the controller using jquery

   

        
3条回答
  •  星月不相逢
    2020-12-01 17:08

    The Way is here.

    If you want specify

    dataType: 'json'

    Then use,

    $('#ddlIssueType').change(function () {
    
    
                var dataResponse = { itemTypeId: $('#ddlItemType').val(), transactionType: this.value };
    
                $.ajax({
                    type: 'POST',
                    url: '@Url.Action("StoreLocationList", "../InventoryDailyTransaction")',
                    data: { 'itemTypeId': $('#ddlItemType').val(), 'transactionType': this.value },
                    dataType: 'json',
                    cache: false,
                    success: function (data) {
                        $('#ddlStoreLocation').get(0).options.length = 0;
                        $('#ddlStoreLocation').get(0).options[0] = new Option('--Select--', '');
    
                        $.map(data, function (item) {
                            $('#ddlStoreLocation').get(0).options[$('#ddlStoreLocation').get(0).options.length] = new Option(item.Display, item.Value);
                        });
                    },
                    error: function () {
                        alert("Connection Failed. Please Try Again");
                    }
                });
    

    If you do not specify

    dataType: 'json'

    Then use

    $('#ddlItemType').change(function () {
    
            $.ajax({
                type: 'POST',
                url: '@Url.Action("IssueTypeList", "SalesDept")',
                data: { itemTypeId: this.value },
                cache: false,
                success: function (data) {
                    $('#ddlIssueType').get(0).options.length = 0;
                    $('#ddlIssueType').get(0).options[0] = new Option('--Select--', '');
    
                    $.map(data, function (item) {
                        $('#ddlIssueType').get(0).options[$('#ddlIssueType').get(0).options.length] = new Option(item.Display, item.Value);
                    });
                },
                error: function () {
                    alert("Connection Failed. Please Try Again");
                }
            });
    

    If you want specify

    dataType: 'json' and contentType: 'application/json; charset=utf-8'

    Then Use

    $.ajax({
                type: 'POST',
                url: '@Url.Action("LoadAvailableSerialForItem", "../InventoryDailyTransaction")',
                data: "{'itemCode':'" + itemCode + "','storeLocation':'" + storeLocation + "'}",
                contentType: "application/json; charset=utf-8",
                dataType: 'json',
                cache: false,
                success: function (data) {
    
                    $('#ddlAvailAbleItemSerials').get(0).options.length = 0;
                    $('#ddlAvailAbleItemSerials').get(0).options[0] = new Option('--Select--', '');
    
                    $.map(data, function (item) {
                        $('#ddlAvailAbleItemSerials').get(0).options[$('#ddlAvailAbleItemSerials').get(0).options.length] = new Option(item.Display, item.Value);
                    });
                },
                error: function () {
                    alert("Connection Failed. Please Try Again.");
                }
            });
    

提交回复
热议问题