Answered by:
THREE TIER CASCADING DROPDOWNLIST ASP.NET MVC5

Question
-
User-366701151 posted
This is the view , the first two dropdownlistts works okay , but the third one (orders) doesnt work, i mean i cant filter orders based on PRODUCT ID , the third dropdownlist is always empty.
@model Northwind.Models.CategoryProduct <br /><br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <br /> <div class="container"> <div class="form-group"> @if (ViewBag.CategoryList != null) { @Html.DropDownListFor(model => model.CategoryID, ViewBag.CategoryList as SelectList, "--Select Category--", new { @class = "form-control" }) } </div> <div class="form-group"> @Html.DropDownListFor(model => model.ProductID, new SelectList(" "), "--Select product--", new { @class = "form-control" }) </div> <div class="form-group"> @Html.DropDownListFor(model => model.OrderID, new SelectList(" "), "--Select order -", new { @class = "form-control" }) </div> </div> <script src="~/Scripts/jquery-1.10.2.min.js"></script> <script> $(document).ready(function () { $("#CategoryID").change(function () { $.get("/Home/GetSProductList", { CategoryID: $("#CategoryID").val() }, function (data) { $("#ProductID").empty(); $.each(data, function (index, row) { $("#ProductID").append("<option value='" + row.ProductID + "'>" + row.ProductName + "</option>") }); }); }) }); $(document).ready(function () { $("#ProductID").change(function () { $.get("/Home/GetOrderList", { CategoryID: $("#ProductID").val() }, function (data) { $("#OrderID").empty(); $.each(data, function (index, row) { $("#OrderID").append("<option value='" + row.OrderID + "'>" + row.OrderID + "</option>") }); }); }) }); </script>
using System; using System.Collections.Generic; using System.Linq; using System.Web; namespace Northwind.Models { public class CategoryProduct { public int CategoryID { get; set; } public int ProductID { get; set; } public int OrderID { get; set; } } }
using System; using System.Collections.Generic; using System.Data; using System.Data.Entity; using System.Linq; using System.Net; using System.Web; using System.Web.Mvc; using Northwind; namespace Northwind.Controllers { public class HomeController : Controller { private dbNorthwindEntities db = new dbNorthwindEntities(); public ActionResult Index() { List<Category> CategoryList = db.Categories.ToList(); ViewBag.CategoryList = new SelectList(CategoryList , "CategoryID" , "CategoryName"); return View(); } public JsonResult GetSProductList(int CategoryID) { db.Configuration.ProxyCreationEnabled = false; List<Product> ProductList = db.Products.Where(x => x.CategoryID == CategoryID).ToList(); return Json(ProductList, JsonRequestBehavior.AllowGet); } public JsonResult GetOrderList(int ProductID) { db.Configuration.ProxyCreationEnabled = false; List<Order_Detail> OrderList = db.Order_Details.Where(x => x.ProductID == ProductID).ToList(); return Json(OrderList, JsonRequestBehavior.AllowGet); } public ActionResult About() { ViewBag.Message = "Your application description page."; return View(); } public ActionResult Contact() { ViewBag.Message = "Your contact page."; return View(); } } }
Tuesday, December 5, 2017 2:00 PM
Answers
-
User991499041 posted
Hi Ddgg,
If you use F12 develop tools, under Network tab, you would see the error.
If you check the action, you would see the parameter is ProductID
public JsonResult GetOrderList(int ProductID)
So to solve this issue, change
CategoryID
toProductID
$("#ProductID").change(function () { $.get("/Home/GetOrderList", { ProductID: $("#ProductID").val() }, function (data) { $("#OrderID").empty(); $.each(data, function (index, row) { $("#OrderID").append("<option value='" + row.OrderID + "'>" + row.OrderID + "</option>") }); }); })
Regards,
zxj
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Wednesday, December 6, 2017 2:03 AM
All replies
-
User991499041 posted
Hi Ddgg,
If you use F12 develop tools, under Network tab, you would see the error.
If you check the action, you would see the parameter is ProductID
public JsonResult GetOrderList(int ProductID)
So to solve this issue, change
CategoryID
toProductID
$("#ProductID").change(function () { $.get("/Home/GetOrderList", { ProductID: $("#ProductID").val() }, function (data) { $("#OrderID").empty(); $.each(data, function (index, row) { $("#OrderID").append("<option value='" + row.OrderID + "'>" + row.OrderID + "</option>") }); }); })
Regards,
zxj
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Wednesday, December 6, 2017 2:03 AM -
User-366701151 posted
are u genius , or something ?!!
Wednesday, December 6, 2017 8:45 AM