Url.Action (action, controller, routeValues) doubles the ID in the URL

I use the following code (taken from a Stackoverflow post: Action Image MVC3 Razor ) in the HTML extension helper to create an action link. My Url.Action () method returns a URL that has routeValues โ€‹โ€‹both on the route and the URL, for example:

/Proposals/List/Tabled?id=Tabled 

when i want just

 /Proposals/List?id=Tabled 

Any suggestions on why he wants to do this?

Update

Route rule from my Global.asax file. That must be why he does it, but why it doubles is still a mystery to me.

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

Update

Here is my method call, and I added the method definition to the code below.

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

Here is my code:

  <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 
+2
source share
1 answer

When you pass routeValues โ€‹โ€‹to the url.action method, it will use the values โ€‹โ€‹to override the existing ones (in the context of the request for the current page).

So, when the current status is Tabled and you do not reset that in the new routes that you pass, it will still use it.

But since you pass the identifier, it also adds this.

You need to go through New With {.id = Model.StatusFilter, .status = nothing}

hierarchy ( from http://forums.asp.net/t/1328683.aspx )

  • The values โ€‹โ€‹specified in the Url.Action call, then
  • The values โ€‹โ€‹specified in the request context for the current page , then
  • The default values โ€‹โ€‹for the route.
+3
source

All Articles