5
Answers

Ajax post request is not loading Item and Data

Hello Team,

In my ajax post request is able to bind the textbox data and the table content except  the Item and the date,  though I can see the item in the table.

 

    function SavePurchases()
    {
            debugger;
           
            var purchaseModel = {};
            var ListOfPurchaseItemsViewModel = new Array();
            purchaseModel.PurchaseID = $.trim($('#PurchaseID').val() + '--' + $('#SelectSupplier').val()),
            purchaseModel.Date = $('#InvocingDate').val().trim(),
            purchaseModel.SupplierID = $('#SelectSupplier').val(),
            purchaseModel.Amount = $('#Amount').val(),
            purchaseModel.Discount = $('#Discount').val(),
            purchaseModel.Tax = $('#Tax').val(),
            purchaseModel.GrandTotal = $('#GrandTotal').val(),
            purchaseModel.IsPaid = $('#Payment').is(":checked") ? 1 : 0,
            purchaseModel.Description = $('#Description').val(),

            $("#orderItems").find("tr:gt(0)").each(function () {
                var PurchaseItemModel = {};
                //PurchaseItemModel.ItemID = parseFloat($(this).find("td:eq(0)").text());
                PurchaseItemModel.ItemName = parseFloat($(this).find("td:eq(0)").text());
                PurchaseItemModel.Batch = parseFloat($(this).find("td:eq(5)").text());
                PurchaseItemModel.Qty = parseFloat($(this).find("td:eq(2)").text());
                PurchaseItemModel.CostPrice = parseFloat($(this).find("td:eq(3)").text());
                PurchaseItemModel.SellingPrice = parseFloat($(this).find("td:eq(4)").text());
                PurchaseItemModel.Expiry = parseFloat($(this).find("td:eq(6)").text());
               
                    PurchaseItemModel.BonusIncluded = 0
                ListOfPurchaseItemsViewModel.push(PurchaseItemModel);
            });
            purchaseModel.ListOfPurchaseItemsViewModel = ListOfPurchaseItemsViewModel;           
                //post data to server
                $.ajax({
                    contentType: "application/json; charset=utf-8",
                    type: "POST",
                    dataType: "JSON",
                    url: "/PurchaseEntry/SavePurchase",
                    data: JSON.stringify(purchaseModel),                   
                    success: function (d) {
                        //check is successfully save to database
                        if (d.status == true) {
                            //will send status from server side
                            alert('Successfully done.');
                            location.reload(true);
                            //clear form
                            purchaseItems = [];
                            $('#PurchaseID').val('');
                            $('#InvocingDate').val('');
                            $('#SelectSupplier').val('0');

                        }
                        else {
                            alert('Failed');
                        }
                        $('#submit').val('Save');
                    },
                    error: function () {
                        alert('Error. Please try again.');
                        $('#btnSubmit').val('Save');
                    }
                });
            
}
   
    function GeneratedItemsTable() {
        if (purchaseItems.length > 0) {
            var $table = $('<table id="mytable" class="table table-striped table-hover"/>');
            $table.append('<thead><tr style="background-color:rgb(201, 211, 218);"><th>Item</th><th>Batch</th><th>Qty</th><th>CP</th><th>SP</th><th>Expiry</th><th>Delete</th></tr></thead>');
            var $tbody = $('<tbody/>');

            // var $table = $('.tableList');
            // var $tbody = $('<tbody/>');

            $.each(purchaseItems, function (i, val) {
                var $row = $('<tr/>');
                //$row.append($('<td/>').html(val.ItemID));
                $row.append($('<td/>').html(val.ItemName));
                $row.append($('<td/>').html(val.Batch));
                $row.append($('<td class="tdQty"/>').html(val.Qty));
                $row.append($('<td class="tdCp"/>').html(val.CostPrice));
                $row.append($('<td/>').html(val.SellingPrice));
                $row.append($('<td/>').html(val.Expiry));
                $row.append($('<td/>').html('<a href=# onclick="removeItem(this)" ><span style="color:red" class="fa fa-trash-alt"></span></a>'));
                $tbody.append($row);
            });
            $table.append($tbody);
            $('#orderItems').html($table);
        }
        else {
            alert("List is empty !");
        }
    }

Answers (5)