Hamza Shah

Hamza Shah

  • NA
  • 87
  • 23k

Select item in dropdown List and appears in table

Oct 12 2020 2:29 AM
I've made two dropdown lists "Company" And "Product".
 
When I select the company, all its products appear in Product Dropdown list.
 
Now I want to do another work. If I select Company and product it will appear on below table list using jquery.
 
I was searching for the solution for last 3 days but can't find it so kindly please help me
 
Controller File
  1. [Authorize][AllowAnonymous]   
  2. public ActionResult Details() {  
  3.   ViewBag.Companies = db.Companies.ToList();  
  4.   return View();  
  5. }  
  6. private IList < Product > GetProduct(int CompanyId) {  
  7.   var data = db.Products.Where(m =>m.CompanyId == CompanyId).ToList();  
  8.   return data;  
  9. } [AcceptVerbs(HttpVerbs.Get)] public JsonResult LoadProductsByCompanyId(string CompanyId, string cn) {  
  10.   Cname = cn;  
  11.   var ProductList = this.GetProduct(Convert.ToInt32(CompanyId));  
  12.   var ProductsData = ProductList.Select(m =>new SelectListItem() {  
  13.     Text = m.ProductName,  
  14.     Value = m.ProductId.ToString(),  
  15.   });  
  16.   return Json(ProductsData, JsonRequestBehavior.AllowGet);  
  17. }  
View File
  1. <script type="text/javascript">  
  2. $(document).ready(function ()  
  3. {  
  4. $("#dd_Company").change(function ()  
  5. {  
  6. var CompanyId = $(this).val();  
  7. // var d = $("#dd_Company").val($(this).text());  
  8. var txt = $("#dd_Company option:selected").text();  
  9. $("#span1").text(txt);  
  10. $.getJSON("../UserLogin/LoadProductsByCompanyId", { CompanyId: CompanyId, cn: txt },  
  11. function (classesData)  
  12. {  
  13. var select = $("#ddProduct");  
  14. select.empty();  
  15. select.append($('<option/>', { value: 0, text: "Select a Product" }));  
  16. $.each(classesData, function (index, itemData)  
  17. {  
  18. select.append($('<option/>', { value: itemData.Value, text: itemData.Text }));  
  19. });  
  20. });  
  21. });  
  22. });  
  23. </script>  
  24. <table>  
  25. <tr>  

Answers (2)