This is a migrated thread and some comments may be shown as answers.

Cascading Dropdownlist action gives error

1 Answer 24 Views
DropDownList
This is a migrated thread and some comments may be shown as answers.
Sid
Top achievements
Rank 1
Sid asked on 03 Mar 2015, 05:01 AM
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?

1 Answer, 1 is accepted

Sort by
0
Dimo
Telerik team
answered on 05 Mar 2015, 06:48 AM
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!
Tags
DropDownList
Asked by
Sid
Top achievements
Rank 1
Answers by
Dimo
Telerik team
Share this question
or