Action link sends Id to different area?
Say you have two areas (Area1
and Area2
) with default routing. Each of these areas has a Home controller.
Say you have two areas (Area1
and Area2
) with default area-aware routing. Each of these areas has a Home controller with an Index. In Area1
's Home/Index view you put:
@Html.ActionLink("Goto Area2", "Index", New With {.area = "Area2"})
and in Area2's Home/Index vie开发者_运维百科w you put:
@Html.ActionLink("Goto Area1", "Index", New With {.area = "Area1"})
Now the problem: If you goto /Area1/Home/Index/1
, the action link tries to send you to /Area2/Home/Index/1
. Why? And how do I get it to not pass that id to the second area?
Thanks for any advice on the matter, its had be stumped for days.
try this...
@Html.ActionLink("Goto Area1", "Index", New With {.area = "Area1", .id="" })
Heres the solution I used. It allows you to specify an area without manually passing ReouteValue's, and the important part is it will automatically ignore route values you didn't specify. This may (and probably does) have errors, so be careful if you use it.
Private ReadOnly ValuesToIgnore As String() = New String() {"action", "controller"}
<Extension()> _
Public Function AreaActionLink(ByVal htmlHelper As HtmlHelper,
ByVal linkText As String, Optional actionName As String = Nothing,
Optional controllerName As String = Nothing,
Optional ByVal areaName As String = Nothing,
Optional ByVal routeValues As Object = Nothing,
Optional htmlAttributes As Object = Nothing) As MvcHtmlString
Dim rvs = If(routeValues IsNot Nothing, New RouteValueDictionary(routeValues), New RouteValueDictionary())
Dim values = htmlHelper.ViewContext.RouteData.Values
If Not String.IsNullOrEmpty(areaName) Then _
rvs("area") = areaName
Dim implicitValues = values.Keys.Except(rvs.Keys)
For Each key In implicitValues.Except(ValuesToIgnore)
rvs(key) = Nothing
Next
Return htmlHelper.ActionLink(
linkText,
actionName,
controllerName,
rvs,
htmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes)
)
End Function
精彩评论