Cascading Dropdownlist action gives error

2 posts, 0 answers
  1. Sid
    Sid avatar
    18 posts
    Member since:
    Oct 2007

    Posted 02 Mar 2015 Link to this post

    Hi,

    I need to setup cascading Dropdownlists for City and Zipcode in my MVC5 controller.  My Action is as follows:
    01.public JsonResult GetZips()
    02.{
    03.    FreightEntities freight = new FreightEntities();
    04.    var zips = freight.ZipCodes.AsQueryable();
    05.    var city = freight.ZipCodes.AsQueryable();
    06.    if (city != null)
    07.    {
    08.        zips = zips.Where(z => z.ZipID == zips);
    09.    }
    10.    return Json(freight.ZipCodes.Select(z => new {Zip = z.ZipID, State = z.StateID, City = z.City }), JsonRequestBehavior.AllowGet);
    11.}
    12. 
    13. 
    14. 
    15.public JsonResult GetCity()
    16.{
    17.    FreightEntities freight = new FreightEntities();
    18.    var state = freight.States.AsQueryable();
    19.    var city = freight.ZipCodes.AsQueryable();
    20.    if (state != null)
    21.    {
    22.        city = city.Where(s => s.StateID == state);
    23.    }
    24.    return Json(freight.ZipCodes.Select(s => new { State = s.StateID, City = s.City }), JsonRequestBehavior.AllowGet);
    25. 
    26.}

    When I build, errors are thrown on the lines 8 and 22 above stating that operator == cannot be applied to operands of type int and System.Linq.IQueryable<FRCV2>.  I followed the dropdownlist example in the Kendo demo section, but it appears that I've not cast correct comparisons?  Can someone identify what I need to change in the above so that I can properly render these cascading dropdownlists?
  2. Dimo
    Admin
    Dimo avatar
    8332 posts

    Posted 05 Mar 2015 Link to this post

    Hello Sid,

    The comparison condition is not correct, hence an error occurs. 

    http://demos.telerik.com/aspnet-mvc/dropdownlist/cascadingdropdownlist

    "zips" and "state" should be values of dropdownlists, and not IQueryable instances. Please inspect our demo once again and let me know if there is anything unclear.


        public JsonResult GetCascadeProducts(int? categories)
        {
            var northwind = new SampleEntities();
            var products = northwind.Products.AsQueryable();
     
            if (categories != null)
            {
                products = products.Where(p => p.CategoryID == categories);
            }
     
            return Json(products.Select(p => new { ProductID = p.ProductID, ProductName = p.ProductName}), JsonRequestBehavior.AllowGet);
        }
     
        public JsonResult GetCascadeOrders(int? products)
        {
            var northwind = new SampleEntities();
            var orders = northwind.Order_Details.AsQueryable();
     
            if (products != null)
            {
                orders = orders.Where(o => o.ProductID == products);
            }
     
            return Json(orders.Select(o => new { OrderID = o.OrderID, ShipCity = o.Order.ShipCity }), JsonRequestBehavior.AllowGet);
        }
    }


    Regards,
    Dimo
    Telerik
    Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
  3. Kendo UI is VS 2017 Ready
Back to Top