Hamza Shah

Hamza Shah

  • NA
  • 87
  • 22.8k

Show Product's Price and Quantity

Oct 19 2020 6:16 AM
Well I'm new in Asp.net MVC and facing problem. I have two dropdown lists "Company" and "Product" When I select company and one of it's product it appears in below table using jquery. Now I want to show it's quantity and price of selected product as well from database. I'm doing this.
 
Here is my Controller Code
  1. [Authorize]  
  2. [AllowAnonymous]  
  3. public ActionResult Details()  
  4. {  
  5. ViewBag.Companies = db.Companies.ToList();  
  6. return View();  
  7. }  
  8. private IList GetProduct(int CompanyId)  
  9. {  
  10. var data = db.Products.Where(m => m.CompanyId == CompanyId).ToList();  
  11. return data;  
  12. }  
  13. [AcceptVerbs(HttpVerbs.Get)]  
  14. public JsonResult LoadProductsByCompanyId(string CompanyId, string cn)  
  15. {  
  16. Cname = cn;  
  17. var ProductList = this.GetProduct(Convert.ToInt32(CompanyId));  
  18. var ProductsData = ProductList.Select(m => new SelectListItem()  
  19. {  
  20. Text = m.ProductName,  
  21. Value = m.ProductId.ToString(),  
  22. });  
  23. return Json(ProductsData, JsonRequestBehavior.AllowGet);  
  24. }  
  25. [AcceptVerbs(HttpVerbs.Post)]  
  26. public JsonResult GetProductById(string ProductId)  
  27. {  
  28. sampledb4Entities db = new sampledb4Entities();  
  29. int pid = Convert.ToInt32(ProductId);  
  30. var Product = db.Products.Where(x => x.ProductId == pid).SingleOrDefault();  
  31. return Json(Product, JsonRequestBehavior.AllowGet);  
  32. }  
And Here Is my View Code
  1. <script>  
  2. $("#ddProduct").change(function () {  
  3. var trr = document.createElement("tr");  
  4. $(trr).append("" + $("#dd_Company option:selected").text() + "").append("" + $("#ddProduct option:selected").text() + "");  
  5. $(".producttable").append(trr);  
  6. });  
  7. <script>  
  8.   
  9. <script>  
  10. $("#ddProduct").change(function () {  
  11. var pid = $(this).val();  
  12. var model = { ProductId: pid };  
  13. $.ajax({  
  14. type: "Post",  
  15. Url: "/UserLogin/GetProductById",  
  16. dataType: "JSON",  
  17. contentType: "application/JSON;charset=utf-8",  
  18. data: JSON.stringify(model),  
  19. cache: false,  
  20. success: function (data) {  
  21. var trr = document.createElement("tr");  
  22. $(trr).append("" + $("#dd_Company option:selected").text() + "").  
  23. append("" + ProductQuantity + "").  
  24. append("" + ProductPrice + "")  
  25. ;  
  26. $(".producttable").append(trr);  
  27. }  
  28. })  
  29. }); </script>  
  30. <div class="producttable">  
  31. <table>  
  32. <tr>  
  33. <th>  
  34. @Html.DisplayNameFor(model => model.CompanyName)  
  35. </th>  
  36. <th>  
  37. @Html.DisplayNameFor(model => model.ProductName)  
  38. </th>  
  39. <th>  
  40. Product Quantity  
  41. </th>  
  42. <th>  
  43. Product Price  
  44. </th>  
  45. </tr>  
  46. </table>  
  47. </div>  

Answers (2)