views:

23

answers:

2

Hei guys i have this JQuery Ajax call from my view and it looks like this:

$("select#Colors").change(function() {
        var color = $("#Colors > option:selected").attr("value");

        $.ajax({
            type: "GET",
            contentType: "application/json; charset=utf-8",
            url: "FindProducts/" + color,
            data: "{}",
            dataType: "json",
            success: function(data) {
               .....
            }
        });

    });

And heres the Action Method in my HomeController

public JsonResult FindProductsByColorID(string color)
{
    // List of Products
    List<Product> productList = new List<Product>{
           new Product{......}
        };

  // return Json result using LINQ to SQL
  return new JsonResult
  {
    Data = (from p in productList
            where p.Color == color
            select p).ToArray<Product>()
  };
}

My goal here is that to call the method FindProductsByColorID using JQuery.ajax. and since the name is a bit lengthy, I registered the url into the global.asax routing table.

routes.MapRoute(
       "FindProducts",
       "FindProducts/{color}",
        new { controller = "Home", action = "FindProductsByColorID", color = ""}
       );

For some reasons the routing didn't happen during the ajax call, when i tested it on Firebug, the URL shows Localhost/Home/FindProducts/Red. Of course the results failed to load coz there is no FindProducts method in the home controller. did i do something wrong with the routing or something? because when i tested this on a new fresh project it works just fine but when i did it on my ongoing project, it just failed. Any solution would be very much appreciated!

+1  A: 

Here is a WAG : Change it to say url: "/FindProducts/" + color, (note the /)

Hogan
oh Wow, i love u dude!
Ari
nice, you're welcome.
Hogan
A: 

Your route is wrong. The route should look like



routes.MapRoute(
                "NameOfRoute",           
                "{controller}/{action}/{parameters}",     
                new { controller = "DefaultContoller", 
                      action = "DefaultMethod", 
                      parameters = { DefaultParameters} }
               );


Rahul
Ari's route is correct, actually you can create any route you want as long as you define your `controller` and `action` in your objects default field and the parameters inside {} are also defined correctly.
rob waminal
yap it is solved actually, just like what Hogan stated above, im missing a "/" =.=
Ari