嘿伙计们,我从我的视图中看到了这个JQuery Ajax调用,它看起来像这样:
$("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) {
.....
}
});
});
继承我的HomeController中的Action Method
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>()
};
}
我的目标是使用JQuery.ajax调用FindProductsByColorID方法。由于名称有点冗长,我将url注册到global.asax路由表中。
routes.MapRoute(
"FindProducts",
"FindProducts/{color}",
new { controller = "Home", action = "FindProductsByColorID", color = ""}
);
由于某些原因,在ajax调用期间没有发生路由,当我在Firebug上测试它时,URL显示Localhost / Home / FindProducts / Red。当然结果无法加载因为家庭控制器中没有FindProducts方法。我做错了路由或什么的?因为当我在一个新的项目上测试它时,它运行得很好但是当我在我正在进行的项目中做到这一点时,它就失败了。任何解决方案都将非常感谢!
答案 0 :(得分:2)
以下是WAG:将其更改为url: "/FindProducts/" + color,
(注意/
)
答案 1 :(得分:0)
你的路线错了。路线应该看起来像
routes.MapRoute(
"NameOfRoute",
"{controller}/{action}/{parameters}",
new { controller = "DefaultContoller",
action = "DefaultMethod",
parameters = { DefaultParameters} }
);