0
Answer

Not Getting value of “Quantity” and “Grand Total”

I've inserting the table's data in my view to database two tables i.e "sales" and "saleitems". I'm getting all the values from table except "Quantity" and "Grand Total". In quantity i have bootstrap "Plus and Minus" button so that i can increase or decrease the quantity. So i want to get that live quantity value. How can i get them please help.
 
Here's my view code
  1. <table id="table" class="table">  
  2. <thead>  
  3. <tr>  
  4. <th scope="col">Product Id</th>  
  5. <th scope="col">Company</th>  
  6. <th scope="col">Product Name</th>  
  7. <th scope="col">User Name</th>  
  8. <th scope="col">User Mobile</th>  
  9. <th scope="col">Price per product</th>  
  10. <th scope="col">Quantity</th>  
  11. <th scope="col">Total Price</th>  
  12. </tr>  
  13. </thead>  
  14. <tbody></tbody>  
  15. <tfoot><tr><td colspan="2">Grand Total : </td><td id="GrandTotal"></td></tr></tfoot>  
  16. </table>  
  17. </div>  
  18. </div>  
  19. <div class="savebutton" >  
  20. <input type="button" id="btnSave" value="Save All"/>  
  21. </div> </div>  
  22. <script>  
  23. $('#productSelect').change(function () {  
  24. var id = $(this).val();  
  25. if (id > 0) {  
  26. $.get("GetProduct", { productId: id }, function (result) {  
  27. console.log(result)  
  28. $("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>")  
  29. CalculateGrandTotal();  
  30. });  
  31. }  
  32. })  
  33. $("body").on("click""#btnSave"function () {  
  34. //Loop through the Table rows and build a JSON array.  
  35. var sales = new Array();  
  36. var saleitems = new Array();  
  37. $(".table tbody tr").each(function () {  
  38. var row = $(this);  
  39. var Sale = {};  
  40. var SaleItem = {};  
  41. Sale.UserName = row.find("td").eq(3).html();  
  42. Sale.UserMobile = row.find("td").eq(4).html();  
  43. Sale.NetTotal = row.find("td").eq(8).html();  
  44. sales.push(Sale);  
  45. SaleItem.ProductId = row.find("td").eq(0).html();  
  46. SaleItem.ProductName = row.find("td").eq(2).html();  
  47. SaleItem.ProductQuantity = row.find("td").eq(7).val();  
  48. saleitems.push(SaleItem);  
  49. });  
  50. var model = { sales:sales , saleitems:saleitems };  
  51. //Send the JSON array to Controller using AJAX.  
  52. $.ajax({  
  53. type: "POST",  
  54. url: "/Product/Insertsales",  
  55. data: JSON.stringify(model),  
  56. contentType: "application/json; charset=utf-8",  
  57. dataType: "json",  
  58. success: function (r) {  
  59. alert(r + " records inserted.");  
  60. }  
  61. });  
  62. });  
  63. </script>
And Here's my Controller code
  1. public JsonResult InsertSales(List<Sale> sales , List<SaleItem> saleitems)  
  2. {  
  3. using (sampledb6Entities sampledb6Entities = new sampledb6Entities())  
  4. {  
  5. //Truncate Table to delete all old records.  
  6. //sampledb6Entities.Database.ExecuteSqlCommand("TRUNCATE TABLE [Sales]");  
  7. //Check for NULL.  
  8. if (sales == null)  
  9. {  
  10. sales = new List<Sale>(); }  
  11. //Loop and insert records.  
  12.   
  13. foreach (Sale sale in sales)  
  14. {  
  15. sampledb6Entities.Sales.Add(sale);  
  16. sampledb6Entities.SaveChanges();  
  17. }  
  18. //Check for NULL.  
  19. if (saleitems == null)  
  20. {  
  21. saleitems = new List<SaleItem>();  
  22. }  
  23. var SaleIds = sales.Select(x => x.SaleId).ToList();  
  24. int Counter = 0;  
  25. foreach (SaleItem saleitem in saleitems) {  
  26. SaleItem si = new SaleItem();  
  27. si.SaleId = SaleIds[Counter];  
  28. si.ProductName = saleitem.ProductName;  
  29. si.ProductQuantity = saleitem.ProductQuantity;  
  30. si.ProductId = saleitem.ProductId;  
  31. sampledb6Entities.SaleItems.Add(saleitem);  
  32. Counter++;  
  33. }  
  34. int insertedRecords = sampledb6Entities.SaveChanges();  
  35. return Json(insertedRecords);  
  36. }  
  37. }