微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

无法获得“数量”和“总计”的值

如何解决无法获得“数量”和“总计”的值

我正在将视图中的表数据插入数据库两个表,即“ sales”和“ saleitems”。我从表中获取除“数量”和“总计”以外的所有值。 在数量上,我具有引导“加号和减号”按钮,以便可以增加或减少数量。我想获得那个活量价值。 如何获取值?

这是我的查看代码


<table id="table" class="table">
                <thead>
                    <tr>
                        <th scope="col">Product Id</th>
                        <th scope="col">Company</th>
                        <th scope="col">Product Name</th>
                        <th scope="col">User Name</th>
                        <th scope="col">User Mobile</th>
                        <th scope="col">Price per product</th>
                        <th scope="col">Quantity</th>
                        <th scope="col">Total Price</th>
                    </tr>
                </thead>
                <tbody></tbody>
                <tfoot><tr><td colspan="2">Grand Total : </td><td id="GrandTotal"></td></tr></tfoot>
            </table>
        </div>
    </div>


    <div class="savebutton" >
        <input type="button" id="btnSave" value="Save All"/>
    </div>
</div>

<script>
        $('#productSelect').change(function () {
            var id = $(this).val();

            if (id > 0) {
                $.get("GetProduct",{ productId: id },function (result) {
                    console.log(result)
                    $("tbody").append("<tr><td>" + result.ProductId + "</td><td>" + result.CompanyName + "</td><td>" + result.ProductName + "</td><td>" + result.UserName + "</td><td>" + result.UserMobile + "</td><td>" + result.ProductPrice + "</td><td><button type='button' class='btn btn-primary' onClick='subtract(" + id + "," + result.ProductPrice + ")'>-</button><button type='button' class='btn btn-dark'  id='" + id + "' value='0'>0</button><button type='button' class='btn btn-primary' onClick='add(" + id + "," + result.ProductPrice + ")'>+</button></td><td id='sum" + id + "'>0</td><td><a onclick='removeRow(this)'>x</a></td></tr>")
                      CalculateGrandTotal();
                    
                });
            }
        })
 $("body").on("click","#btnSave",function () {
        //Loop through the Table rows and build a JSON array.
        var sales = new Array();
        var saleitems = new Array();
        $(".table tbody tr").each(function () {
            var row = $(this);
            var Sale = {};
            var SaleItem = {};
            
            Sale.UserName = row.find("td").eq(3).html();
            Sale.UserMobile = row.find("td").eq(4).html();
            Sale.NetTotal = row.find("td").eq(8).html();
            
            sales.push(Sale);

            
            SaleItem.ProductId = row.find("td").eq(0).html();
            SaleItem.ProductName = row.find("td").eq(2).html();
            SaleItem.ProductQuantity = row.find("td").eq(7).val();
            saleitems.push(SaleItem);
        });


        var model = { sales:sales,saleitems:saleitems };
        //Send the JSON array to Controller using AJAX.
        $.ajax({
            type: "POST",url: "/Product/Insertsales",data: JSON.stringify(model),contentType: "application/json; charset=utf-8",dataType: "json",success: function (r) {
                alert(r + " records inserted.");
            }
        });
    });

这是我的控制器代码

public JsonResult InsertSales(List<Sale> sales,List<SaleItem> saleitems)
        {
            using (sampledb6Entities sampledb6Entities = new sampledb6Entities())
            {
                //Truncate Table to delete all old records.
                //sampledb6Entities.Database.ExecutesqlCommand("TruncATE TABLE [Sales]");

                //Check for NULL.
                if (sales == null)
                {
                     sales = new List<Sale>();
                     
                }

                //Loop and insert records.
                foreach (Sale sale in sales)
                {
                    sampledb6Entities.Sales.Add(sale);
                    sampledb6Entities.SaveChanges();
                }

                //Check for NULL.
               
                
               
                if (saleitems == null)
                {
                    saleitems = new List<SaleItem>();

                }
                var SaleIds = sales.Select(x => x.SaleId).ToList();
                int Counter = 0;
                foreach (SaleItem saleitem in saleitems)
                {

                    SaleItem si = new SaleItem();
                    si.SaleId = SaleIds[Counter];
                    si.ProductName = saleitem.ProductName;
                    si.ProductQuantity = saleitem.ProductQuantity;
                    si.ProductId = saleitem.ProductId;
                    sampledb6Entities.SaleItems.Add(saleitem);
                    Counter++;
                   
                }
                int insertedRecords = sampledb6Entities.SaveChanges();
                return Json(insertedRecords);
            }
        }

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。