我在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
什麼是填充到routeValues? – Chandu 2011-04-20 18:08:08
對不起,只是這樣一個不可忽略的類型: 新的與{.id =「tabled」} – 2011-04-20 18:10:31
你應該告訴我們你的路線規則。 – 2011-04-20 18:19:32