2010-05-06 33 views
0

我試圖刪除http://localhost:1985/Materials/Details/2/SteelDetails,但我的一些路線似乎並不怎麼工作的?「詳細信息」 asp.net的MVC路由似乎並沒有得到刪除

編輯:

routes.MapRoute(
      "Materials", // <-- Above default 
      "Materials/{id}/{name}", 
      new { controller = "Materials", action = "Details", id = "", name = "" } 
     ); 

     routes.MapRoute(
      "Default", // <-- Last route, kind of a "catch all" 
      "{controller}/{action}/{id}/{name}", 
      new { controller = "Materials", action = "Index", id = "", name = "" } 
     ); 

下面放置在我的路由集合回答我的索引頁沒有打電話給jsonresult控制器的方法....

public class MaterialsController : Controller 
    { 
    public ActionResult Index() 
    { 
     return View("Materials"); 
    } 

    public JsonResult GetMaterials(int currentPage,int pageSize) 
    { 
     var materials = consRepository.FindAllMaterials().AsQueryable(); 
     var count = materials.Count(); 
     var results = new PagedList<MaterialsObj>(materials, currentPage-1, pageSize); 
     var genericResult = new { Count = count, Results = results }; 
     return Json(genericResult); 
    } 
    } 

和我的索引頁有一個使用JSON結果jQuery的功能....

<script type="text/javascript"> 
$(document).ready(function() { 
    $.ajax({ 
    url: "Materials/GetMaterials", 
    data: {'currentPage': (currentPage + 1) ,'pageSize':5}, 
    contentType: "application/json; charset=utf-8", 

這個jQuery功能似乎並沒有調用jsonresult控制方法......但是,如果我先指定Default其路由工程...

當通過螢火蟲考察它顯示了這一點,

The parameters dictionary contains a null entry for parameter 'id' of non-nullable type 'System.Int32' for method 'System.Web.Mvc.ActionResult Details(Int32)' in 'CrMVC.Controllers.MaterialsController'. To make a parameter optional its type should be either a reference type or a Nullable type.<br>Parameter name: parameters

+0

看起來像這個一樣的問題http://stackoverflow.com/questions/2230165/asp-net-mvc-2-one-route-works-one-route-doesnt – R0MANARMY 2010-05-06 05:18:10

回答

1

你的路線是向下。它們的順序是matched,它們被添加到集合中,所以默認路由首先被匹配。


編輯

routes.MapRoute(
    "Materials", 
    "Materials/{id}/{name}", 
    new { controller = "Materials", action = "Details", id = "", name = "" }, 
    new {id= @"\d+" } // Prevent routes in the form of {controller}/{action}/{id} from matching. 
); 

routes.MapRoute(
    "Default", 
    "{controller}/{action}/{id}/{name}",       
    new { controller = "Materials", action = "Index", id = "" ,name=""} 
); 
+0

@Romanarmy我應該改變什麼?路線1或路線2? – 2010-05-06 05:18:30

+0

@Romanary ya剛剛看到鏈接... – 2010-05-06 05:29:22

+0

@Pandiya Chendur:增加了一個我的意思是「你的路線向後」的例子。 HTH – R0MANARMY 2010-05-06 06:13:24

1

嘗試使用routelink幫手生成此請求的正確網址,這樣你就可以檢查是否有它完全正確的,然後使用該網址在你的JavaScript 。有可能您的網址不太正確,導致問題。

您可以使用routelink助手在鏈接上放置一個URL,以獲取它並查看它應該是什麼樣子 - 然後將其與AJAX請求中的實際URL進行比較。

+0

@Sohnee路由鏈接助手的任何示例.... – 2010-05-06 11:58:23

+0

在您的視圖中,鍵入「Html.RouteLink(」,它會提示你的細節。希望這有助於。 – Fenton 2010-05-06 15:25:45

+0

感謝您的更新...我絕對會嘗試這... – 2010-05-07 04:00:38