ASP.NET MVC Master Detail Entry Form

后端 未结 6 1221
野趣味
野趣味 2021-01-30 18:02

I’m trying to implement an order entry form using ASP.NET MVC but facing a lot of difficulties. All the samples that I found are related to viewing master detail forms, and non

6条回答
  •  无人共我
    2021-01-30 18:38

    Step 1: Create view model

    public class OrderVM
    {
        public string OrderNo { get; set; }
        public DateTime OrderDate { get; set; }
        public string Description { get; set; }
        public List OrderDetails {get;set;}
    }
    

    Step 2: Add javascript for add order lines

    
    

        $(function () {
            $('#orderDate').datepicker({
                dateFormat : 'mm-dd-yy'
            });
        });
    
        $(document).ready(function () {
            var orderItems = [];
            //Add button click function
            $('#add').click(function () {
                //Check validation of order item
                var isValidItem = true;
                if ($('#itemName').val().trim() == '') {
                    isValidItem = false;
                    $('#itemName').siblings('span.error').css('visibility', 'visible');
                }
                else {
                    $('#itemName').siblings('span.error').css('visibility', 'hidden');
                }
    
                if (!($('#quantity').val().trim() != '' && !isNaN($('#quantity').val().trim()))) {
                    isValidItem = false;
                    $('#quantity').siblings('span.error').css('visibility', 'visible');
                }
                else {
                    $('#quantity').siblings('span.error').css('visibility', 'hidden');
                }
    
                if (!($('#rate').val().trim() != '' && !isNaN($('#rate').val().trim()))) {
                    isValidItem = false;
                    $('#rate').siblings('span.error').css('visibility', 'visible');
                }
                else {
                    $('#rate').siblings('span.error').css('visibility', 'hidden');
                }
    
                //Add item to list if valid
                if (isValidItem) {
                    orderItems.push({
                        ItemName: $('#itemName').val().trim(),
                        Quantity: parseInt($('#quantity').val().trim()),
                        Rate: parseFloat($('#rate').val().trim()),
                        TotalAmount: parseInt($('#quantity').val().trim()) * parseFloat($('#rate').val().trim())
                    });
    
                    //Clear fields
                    $('#itemName').val('').focus();
                    $('#quantity,#rate').val('');
    
                }
                //populate order items
                GeneratedItemsTable();
    
            });
            //Save button click function
            $('#submit').click(function () {
                //validation of order
                var isAllValid = true;
                if (orderItems.length == 0) {
                    $('#orderItems').html('<span style="color:red;">Please add order items</span>');
                    isAllValid = false;
                }
    
                if ($('#orderNo').val().trim() == '') {
                    $('#orderNo').siblings('span.error').css('visibility', 'visible');
                    isAllValid = false;
                }
                else {
                    $('#orderNo').siblings('span.error').css('visibility', 'hidden');
                }
    
                if ($('#orderDate').val().trim() == '') {
                    $('#orderDate').siblings('span.error').css('visibility', 'visible');
                    isAllValid = false;
                }
                else {
                    $('#orderDate').siblings('span.error').css('visibility', 'hidden');
                }
    
                //Save if valid
                if (isAllValid) {
                    var data = {
                        OrderNo: $('#orderNo').val().trim(),
                        OrderDate: $('#orderDate').val().trim(),
                        //Sorry forgot to add Description Field
                        Description : $('#description').val().trim(),
                        OrderDetails : orderItems
                    }
    
                    $(this).val('Please wait...');
    
                    $.ajax({
                        url: '/Home/SaveOrder',
                        type: "POST",
                        data: JSON.stringify(data),
                        dataType: "JSON",
                        contentType: "application/json",
                        success: function (d) {
                            //check is successfully save to database 
                            if (d.status == true) {
                                //will send status from server side
                                alert('Successfully done.');
                                //clear form
                                orderItems = [];
                                $('#orderNo').val('');
                                $('#orderDate').val('');
                                $('#orderItems').empty();
                            }
                            else {
                                alert('Failed');
                            }
                            $('#submit').val('Save');
                        },
                        error: function () {
                            alert('Error. Please try again.');
                            $('#submit').val('Save');
                        }
                    });
                }
    
            });
            //function for show added items in table
            function GeneratedItemsTable() {
                if (orderItems.length > 0) {
                    var $table = $('<table/>');
                    $table.append('<thead><tr><th>Item</th><th>Quantity</th><th>Rate</th><th>Total</th></tr></thead>');
                    var $tbody = $('<tbody/>');
                    $.each(orderItems, function (i, val) {
                        var $row = $('<tr/>');
                        $row.append($('<td/>').html(val.ItemName));
                        $row.append($('<td/>').html(val.Quantity));
                        $row.append($('<td/>').html(val.Rate));
                        $row.append($('<td/>').html(val.TotalAmount));
                        $tbody.append($row);
                    });
                    $table.append($tbody);
                    $('#orderItems').html($table);
                }
            }
        });
    
    
    

    Step 3: Create an action for save data

    [HttpPost]
        public JsonResult SaveOrder(OrderVM O)
        {
            bool status = false;
            if (ModelState.IsValid)
            {
                using (MyDatabaseEntities dc = new MyDatabaseEntities())
                {
                    Order order = new Order { OrderNo = O.OrderNo, OrderDate = O.OrderDate, Description = O.Description };
                    foreach (var i in O.OrderDetails)
                    {
                        //
                       // i.TotalAmount = 
                        order.OrderDetails.Add(i);
                    }
                    dc.Orders.Add(order);
                    dc.SaveChanges();
                    status = true;
                }
            }
            else
            {
                status = false;
            }
            return new JsonResult { Data = new { status = status} };
        }
    

    you can download source code and video tutorial

提交回复
热议问题