2010-12-18 100 views
4

我有一個問題,MVC-3爲我生成傳出路由。問題ViewBag和路由(MVC 3 - RC2)

這是頁面的我在這兩種方案地址:http://localhost:1283/Conflict/Create/1200/300

這裏有地圖路線:

routes.MapRoute(
    null, // Route name 
    "{controller}/{action}/{custId}/{projId}", // URL with parameters 
    null, // Parameter defaults 
    new { custId = @"\d+", projId = @"\d+" } 
); 

routes.MapRoute(
    null, // Route name 
    "{controller}/{action}/{id}", // URL with parameters 
    new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults 
); 

方案1:

從控制器:

public ActionResult Create(int custId, int projId) 
{ 
    return View(); 
} 

從視圖:

@Html.ActionLink("Back to List", "Index", "Conflict", new { custId = ViewBag.custId, projId = ViewBag.projId }, null) 

生成的結果鏈接。

http://localhost:1283/Conflict?custId=1200&projId=300

如果我改變控制器代碼如下:

public ActionResult Create(int custId, int projId) 
{ 
     ViewBag.custId = custId; 
     ViewBag.projId = projId; 

     return View(); 
} 

我並沒有在視圖中的任何改變,只加這兩條線的控制器和下面的鏈接創建:

http://localhost:1283/Conflict/Index/1200/300

缺少什麼我在這裏?這是一貫的行爲,我能夠在我的應用程序的其他領域重現這一點。 「解決方案」是顯而易見的,但我的問題是爲什麼?

+0

我無法在我的機器上重現此行爲!奇怪 – swapneel 2011-03-08 20:39:56

+0

索引操作方法還是創建?在你的actionlink中,第二個參數是「Index」? – 2011-03-17 12:54:32

回答

2

發生了什麼事是「?custId = 1200 & projId = 300」鏈接的一部分將從您用於獲取您所在頁面的鏈接中過來。所以Html.ActionLink調用是這樣做的:

  1. 生成/衝突/索引路徑
  2. 認準CUSTID和PROJID在ViewBag,發現查詢字符串,而不是
  3. 只是附加您的查詢字符串

在第二種情況下,實際上是提供值,所以鏈接正常生成。希望有所幫助。