Click here to Skip to main content
15,889,909 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
Problem
orderItems list store only last record and ignore another rows problem when click save button why ?
meaning
CSS
saleslino  quantity
1             2
2             6
3             9

it must store all in list but actually it store wrong
it store last record only meaning it store 3 and its quantity 9 .
my model as following

C#
public class SalesHeader
    {
        public int SalesOrderNo { get; set; }
        public int SalesYear { get; set; }
        public int BranchCode { get; set; }
        public int SalesType { get; set; }
        public DateTime SalesDate { get; set; }
        public int CustomerID { get; set; }
        public Customer Customers { get; set; }
        public ICollection<SalesFooter> SalesFooters { get; set; }
        public decimal TotalPrice { get; set; }
    }


What I have tried:

<script>
    $(document).ready(function () {
        var orderItems = [];
        //Add button click function
        $('#add').click(function () {
            //Check validation of order item
            var isValidItem = true;
            
            //Add item to list if valid
            if (isValidItem) {
                orderItems.push({
                    SalesLineNo: $('#SalesLineNo').val().trim(),
                    ItemCode: $('#ItemCode').val().trim(),
                    Quantity: parseInt($('#Qunatity').val().trim()),
                    UnitPrice: parseFloat($('#UnitPrice').val().trim()),
                    Total: parseInt($('#Qunatity').val().trim()) * parseFloat($('#UnitPrice').val().trim())
                });
            }
            //populate order items
            GeneratedItemsTable();

        });
        function GeneratedItemsTable() {
            if (orderItems.length > 0) {
                var $table = $('<table/>');
                $table.append('<thead><tr><th>SalesLineNo</th><th>ItemCode</th><th>Qunatity</th><th>UnitPrice</th><th>Total</th><th></th></tr></thead>');
                var $tbody = $('<tbody/>');
                $.each(orderItems, function (i, val) {
                    var $row = $('<tr/>');
                    $row.append($('<td/>').html(val.SalesLineNo));
                    $row.append($('<td/>').html(val.ItemCode));
                    $row.append($('<td/>').html(val.Qunatity));
                    $row.append($('<td/>').html(val.UnitPrice));
                    $row.append($('<td/>').html(val.Total));
                    var $remove = $('<a href="#">Remove</a>');
                    $remove.click(function (e) {
                        e.preventDefault();
                        orderItems.splice(i, 1);
                        GeneratedItemsTable();
                    });
                    $row.append($('<td/>').html($remove));
                    $tbody.append($row);
                });
                console.log("current", orderItems);
                $table.append($tbody);
                $('#orderItems').html($table);
            }
            else {
                $('#orderItems').html('');
            }
        }
        $("#saveOrder").click(function (e) {
            e.preventDefault();

            //================Save Footer====
            var orderItems = new Array();

            var orderItem = {};
            orderItem.SalesLineNo = $('#SalesLineNo').val().trim();
            orderItem.ItemCode = $('#ItemCode').val().trim();
            orderItem.Quantity = parseInt($('#Qunatity').val().trim());
            orderItem.Price = parseFloat($('#UnitPrice').val().trim());
            orderItem.Total = parseInt($('#Qunatity').val().trim()) * parseFloat($('#UnitPrice').val().trim());
            orderItems.push(orderItem);
            //===========Save Header=======
            var saleheader = {};
            saleheader.SalesOrderNo = $("#SalesOrderNo").val();
            saleheader.SalesYear = $("#SalesYear").val();
            saleheader.BranchCode = $("#BranchCode").val();
            saleheader.SalesType = $("#SalesType").val();
            saleheader.SalesDate = $("#SalesDate").val();
            saleheader.TotalPrice = $("#TotalPrice").val();
            saleheader.CustomerID = $("#CustomerID").val();
            saleheader.SalesFooters = orderItems;
            //===========

            console.log('Submit data: ');
            console.log(JSON.stringify(saleheader));
            $.ajax({
                traditional: true,
                //contentType: 'application/json; charset=utf-8',
                contentType: 'application/json',
                dataType: 'json',
                type: 'POST',
                url: '/SalesOrder/Create',
                data: JSON.stringify(saleheader),
                success: function (result) {
                    console.log('Data received: ');
                    alert(result);
                    location.reload();
                },
                error: function () {
                    alert("Error!")
                }
            });
        });
    });
   
</script>
Posted
Comments
Bryian Tan 21-Jan-19 23:25pm    
What the code suppose to do? passing the orderItems or saleHeader to SalesOrder Controller?
ahmed_sa 22-Jan-19 18:58pm    
[HttpPost]
public IActionResult Create([FromBody]SalesHeader saleheader)
{
}
if i put breakpoint here when click save button
it show to me null
why
it must have one record in header
and two rows on footer
https://www.mediafire.com/view/jbc22aojyo2nbbr/nullresultdisplay.jpg/file
ahmed_sa 22-Jan-19 18:59pm    
show null on saleheader object

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900