2011-04-20 55 views
2

我在HTML擴展助手中使用以下代碼(取自Stackoverflow文章:Action Image MVC3 Razor)以構建操作鏈接。當我想要的只是Url.Action(action,controller,routeValues)在URL中加倍ID

/Proposals/List?id=Tabled 

任何

/Proposals/List/Tabled?id=Tabled 

:我Url.Action()方法返回了在路由的routeValues雙方以及附加到URL,像這樣的URL爲什麼要這樣做的建議?

更新

的路由規則,從我Global.asax文件。這一定是它爲什麼這樣做的原因,但爲什麼它會翻倍,對我來說仍然是一個謎。

routes.MapRoute(_ 
    "ProposalsList", _ 
    "Proposals/List/{status}", _ 
    New With {.controller = "Proposals", .action = "List", .status = "Pending"} _ 
    ) 

更新

這裏是我的方法調用,和我添加的方法定義,下面的代碼。

@Html.ActionImage("Proposals", "List", New With {.id = Model.StatusFilter}, "~/images/" + Model.ImageFile, "Count", 32, 32, Model.ProposalsCount.ToString + " " + Model.StatusFilter + " Proposal(s)") 

這裏是我的代碼:

<Extension()> _ 
    Public Function ActionImage(ByVal html As HtmlHelper, ByVal controller As String, ByVal action As String, ByVal routeValues As Object, ByVal imagePath As String, ByVal alt As String, ByVal width As Integer, ByVal height As Integer, ByVal text As String) As MvcHtmlString     
      Dim url = New UrlHelper(html.ViewContext.RequestContext) 
      Dim imgHtml As String 
      Dim anchorHtml As String 
      Dim imgbuilder = New TagBuilder("img") 

      imgbuilder.MergeAttribute("src", url.Content(imagePath)) 
      imgbuilder.MergeAttribute("alt", alt) 
      imgbuilder.MergeAttribute("width", width) 
      imgbuilder.MergeAttribute("height", height) 
      imgHtml = imgbuilder.ToString(TagRenderMode.SelfClosing) 

      Dim anchorBuilder = New TagBuilder("a") 
      anchorBuilder.MergeAttribute("href", url.Action(action, controller, routeValues)) 
      anchorBuilder.InnerHtml = imgHtml + "<br/>" + text 
      anchorHtml = anchorBuilder.ToString(TagRenderMode.Normal) 

      Return MvcHtmlString.Create(anchorHtml) 
    End Function 
+0

什麼是填充到routeValues? – Chandu 2011-04-20 18:08:08

+0

對不起,只是這樣一個不可忽略的類型: 新的與{.id =「tabled」} – 2011-04-20 18:10:31

+1

你應該告訴我們你的路線規則。 – 2011-04-20 18:19:32

回答

3

當您通過routeValues到url.action方法會使用的值來覆蓋當前定義的那些(在請求上下文中的當前頁) 。

所以噹噹前狀態爲Tabled,你不復位,在您通過新routeValues,那麼它仍然會使用它..

但既然你傳遞一個id以及它補充說,太..

你需要傳遞New With {.id = Model.StatusFilter, .status = nothing}

的層次結構(http://forums.asp.net/t/1328683.aspx

在Url.Action調用中指定個
  1. 值,則在請求上下文中爲當前頁面指定
  2. 值,然後
  3. 默認值的路由。
+0

啊哈!很棒的信息,謝謝Gaby! – 2011-04-20 18:44:49

+0

實際上,我認爲我真正需要做的是回顧我的方法並簡化這裏的事情。我是MVC的新手,爲自己想要完成的事情而煩惱。 – 2011-04-20 18:46:05

相關問題