Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • jared 2 posts 92 karma points
    Jun 21, 2023 @ 13:15
    jared
    0

    Url.Action route values removed

    When I use Url.Action in a view it strips any route values I've added. Should I be using something else? I'm trying to do pagination on V11.

    Example: Url.Action("Index", new { page = 2 }) should render a link like /controller/index?page=2 but page is stripped out and I end up with /controller/index.

    My controller index method looks like this below and can receive "page" if i enter the url manually in my browser. public IActionResult Index([FromQuery(Name = "page")] int page = 1){ ... }

  • jared 2 posts 92 karma points
    Jun 23, 2023 @ 12:17
    jared
    100

    I ended up creating a helper since url.action will not work here.

        public static string UmbracoUrl(this IHtmlHelper htmlHelper, string url, object routeValues)
        {
            if (routeValues != null)
            {
                Dictionary<string, string> values = routeValues.GetType().GetProperties()
                    .ToDictionary(x => x.Name, x => x.GetValue(routeValues)?.ToString() ?? "");
    
                if (values.Any())
                {
                    string delimiter = "?";
                    foreach (var routeValue in values)
                    {
                        url += delimiter + routeValue.Key + "=" + routeValue.Value.ToString();
                        delimiter = "&";
                    }
                }
    
            }
    
            return url;
        }
    
Please Sign in or register to post replies

Write your reply to:

Draft