asp.net mvc Html.ActionLink() keeping route value I don't want

后端 未结 9 1761
我寻月下人不归
我寻月下人不归 2020-11-30 04:34

I have the following ActionLink in my view

<%= Html.ActionLink(\"LinkText\", \"Action\", \"Controller\"); %>

and it creates the follo

相关标签:
9条回答
  • 2020-11-30 04:56

    Another way is to use ActionLink(HtmlHelper, String, String, RouteValueDictionary) overload, then there are no need to put null in the last parameter

    <%= Html.ActionLink("Details", "Details", "Product", new RouteValueDictionary(new { id=item.ID })) %>
    
    0 讨论(0)
  • 2020-11-30 05:01

    The overloads of Html.ActionLink are changed on the later versions of MVC. On MVC 5 and above. This is how to do this:

    @Html.ActionLink("LinkText", "Action", "Controller", new { id = "" }, null)
    

    Note I passed "" for id parameter and null for HTMLATTRIBUTES.

    0 讨论(0)
  • 2020-11-30 05:10

    If you either don't know what values need to be explicitly overridden or you just want to avoid the extra list of parameters you can use an extension method like the below.

    <a href="@Url.Isolate(u => u.Action("View", "Person"))">View</a>
    

    The implementation details are in this blog post

    0 讨论(0)
  • 2020-11-30 05:12

    The solution is to specify my own route values (the third parameter below)

    <%= Html.ActionLink("LinkText", "Action", "Controller", 
        new { id=string.Empty }, null) %>
    
    0 讨论(0)
  • 2020-11-30 05:13

    It sounds like you need to register a second "Action Only" route and use Html.RouteLink(). First register a route like this in you application start up:

    routes.MapRoute("ActionOnly", "{controller}/{action}", 
       new { controller = "Home", action = "Index" } );
    

    Then instead of ActionLink to create those links use:

    Html.RouteLink("About","ActionOnly")
    
    0 讨论(0)
  • 2020-11-30 05:13

    Don't know why, but it didn't work for me (maybe because of Mvc2 RC). Created urlhelper method =>

     public static string
                WithoutRouteValues(this UrlHelper helper, ActionResult action,params string[] routeValues)
            {
                var rv = helper.RequestContext.RouteData.Values;
                var ignoredValues = rv.Where(x=>routeValues.Any(z => z == x.Key)).ToList();
                foreach (var ignoredValue in ignoredValues)
                    rv.Remove(ignoredValue.Key);
                var res = helper.Action(action);
                foreach (var ignoredValue in ignoredValues)
                    rv.Add(ignoredValue.Key, ignoredValue.Value);
                return res;
            }
    
    0 讨论(0)
提交回复
热议问题