Hamza Shah

Hamza Shah

  • NA
  • 87
  • 21.2k

Value of foreign key in not coming in the foreign table

Nov 4 2020 12:19 AM
I have two database tables i.e "Sales" and "SaleItems". SaleId is a foreign key in SaleItems but i'm not getting the value. I'm pushing the "Product" database table's data to both "Sales" and "Saleitems". SaleId is generating in "Sales" table but i'm not getting it in "SaleItems" table. kindly 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. <script>  
  18. $('#productSelect').change(function () {  
  19. var id = $(this).val();  
  20. if (id > 0) {  
  21. $.get("GetProduct", { productId: id }, function (result) {  
  22. console.log(result)  
  23. $("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='text' 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>")  
  24. CalculateGrandTotal();  
  25. });  
  26. }  
  27. })  
  28. $("body").on("click""#btnSave"function () {  
  29. //Loop through the Table rows and build a JSON array.  
  30. var sales = new Array();  
  31. var saleitems = new Array();  
  32. $(".table tbody tr").each(function () {  
  33. var row = $(this);  
  34. var Sale = {};  
  35. Sale.UserName = row.find("td").eq(3).html();  
  36. Sale.UserMobile = row.find("td").eq(4).html();  
  37. Sale.NetTotal = $('#GrandTotal').html();  
  38. sales.push(Sale);  
  39. var SaleItem = {};  
  40. SaleItem.ProductId = row.find("td").eq(0).html();  
  41. SaleItem.ProductName = row.find("td").eq(2).html();  
  42. SaleItem.ProductQuantity = row.find("td").eq(7).find('button[type=text]').html();  
  43. saleitems.push(SaleItem);  
  44. });  
  45. var model = { sales:sales , saleitems:saleitems };  
  46. //Send the JSON array to Controller using AJAX.  
  47. $.ajax({  
  48. type: "POST",  
  49. url: "/Product/Insertsales",  
  50. data: JSON.stringify(model),  
  51. contentType: "application/json; charset=utf-8",  
  52. dataType: "json",  
  53. success: function (r) {  
  54. alert(r + " records inserted.");  
  55. }  
  56. });  
  57. });  
  58. </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. foreach (Sale sale in sales)  
  13. {  
  14. sampledb6Entities.Sales.Add(sale);  
  15. sampledb6Entities.SaveChanges();  
  16. }  
  17. //Check for NULL.  
  18. if (saleitems == null) {  
  19. saleitems = new List<SaleItem>();  
  20. }  
  21. var SaleIds = sales.Select(x => x.SaleId).ToList();  
  22. int Counter = 0;  
  23. foreach (SaleItem saleitem in saleitems)  
  24. {  
  25. SaleItem si = new SaleItem();  
  26. si.SaleId = SaleIds[Counter];  
  27. si.ProductName = saleitem.ProductName;  
  28. si.ProductQuantity = saleitem.ProductQuantity;  
  29. si.ProductId = saleitem.ProductId;  
  30. sampledb6Entities.SaleItems.Add(saleitem);  
  31. Counter++;  
  32. }  
  33. int insertedRecords = sampledb6Entities.SaveChanges();  
  34. return Json(insertedRecords);  
  35. }  
  36. }  

Answers (2)