2010-09-01 76 views
0

喜家伙我从我的观点这个jQuery Ajax调用,它看起来像这样:路由在Global.asax中不注册

$("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

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的过程中并没有发生,该网址显示本地主机/主页/ FindProducts /红色。当然,结果未能加载因为在家庭控制器中没有FindProducts方法。我是否在路由选择方面做错了什么?因为当我在一个新的新项目上测试它时,它工作得很好,但是当我在正在进行的项目中完成时,它只是失败了。任何解决方案将非常感谢!

回答

2

这里是一个WAG:将其更改为说url: "/FindProducts/" + color, (注意/

+0

哦哇,我爱你的家伙! – Ari 2010-09-01 03:22:31

+0

不错,不客气。 – Hogan 2010-09-01 03:23:33

0

您的路线是错误的。路线应该看起来像

 


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

 
+0

Ari的路线是正确的,实际上,只要您在对象的默认字段中定义了“控制器”和“操作”,并且{}内的参数也可以正确定义,就可以创建任何您想要的路线。 – 2010-09-01 03:09:59

+0

yap它实际上解决了,就像Hogan上面所说的那样,即时错过了“/”=。= – Ari 2010-09-01 03:24:07