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 cal开发者_运维问答l 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!
Here is a WAG : Change it to say url: "/FindProducts/" + color,
(note the /
)
Your route is wrong. The route should look like
routes.MapRoute(
"NameOfRoute",
"{controller}/{action}/{parameters}",
new { controller = "DefaultContoller",
action = "DefaultMethod",
parameters = { DefaultParameters} }
);
精彩评论