Paging and sorting grid in ASP.NET MVC

I am trying to implement a swap and sort list in ASP.NET MVC without using the MVContrib grid or javascript (should be seo-friendly).

I created my action with the following signature:

ActionResult List(int? page, string sort, string direction);

The problem that I have is that both swapping and sorting work. Say, for example, I have the following code:

<%= Html.ActionLink("Title", "List", new { sort = "Title", direction = "ASC" }) %>

I was hoping this would lead to a URL containing all existing route values ​​(including the current page), but that is not the case. Therefore, when you click on the link, the page returns to zero.

I looked through all the overloads for the ActionLink helper, but nothing of the kind will help. What I really need to do is create a url / link with an existing page value (or possibly any other route values) and new sorting options.

I would be grateful if someone could help. Thank.

+3
source share
3 answers

The problem is resolved. I managed to find the following extension methods:

public static class ActionLinkExtensions
{
    public static MvcHtmlString ActionLink(this HtmlHelper helper, string linkText, string actionName, bool addExistingRouteValues)
    {
        return ActionLink(helper, linkText, actionName, null, null, addExistingRouteValues, null);
    }

    public static MvcHtmlString ActionLink(this HtmlHelper helper, string linkText, string actionName, object routeValues, bool addExistingRouteValues)
    {
        return ActionLink(helper, linkText, actionName, null, routeValues, addExistingRouteValues, null);
    }

    public static MvcHtmlString ActionLink(this HtmlHelper helper, string linkText, string actionName, string controllerName, object routeValues, bool addExistingRouteValues, object htmlAttributes)
    {
        var queryString = helper.ViewContext.HttpContext.Request.QueryString;
        var newRouteValues = routeValues == null ? helper.ViewContext.RouteData.Values : new RouteValueDictionary(routeValues);

        if (addExistingRouteValues)
        {
            foreach (string key in queryString.Keys)
            {
                if (!newRouteValues.ContainsKey(key))
                    newRouteValues.Add(key, queryString[key]);
            }
        }

        return MvcHtmlString.Create(HtmlHelper.GenerateLink(helper.ViewContext.RequestContext, helper.RouteCollection, linkText, null, actionName, controllerName, newRouteValues, new RouteValueDictionary(htmlAttributes)));
    }
}

Now you need to add true at the end of your helper, and existing route values ​​will be added. Hope this helps.

0
source

You will need to write your own html helper, which is easy to do. To do this, use the following format:

public static class MyExtensions
{
   public static string MyActionLink(this HtmlHelper html, ...)
   {
      //Definition, return string
   }
}

, . HTML-, ( ...

.

0

Why can't you specify the page in your route values ​​as follows?

<%= Html.ActionLink("Title", "List", new { page = currentPage, sort = "Title", direction = "ASC" }) %>

I will be afraid that the model for ActionResult List(int? page, string sort, string direction);is your list of items at the moment, so you do not have the current page. In this case, create a view model containing your list and the current file. eg.

// View Model
public class MyViewModel
{
    public int CurrentPage { get; set; }
    public IList<SomeObject> Items { get; set; }
}

// Action Method
public ActionResult List(int? page, string sort, string direction)
{
    return View(new MyViewModel()
    {
        CurrentPage = page ?? 1,
        Items       = _myRepository.GetPagedList(page, sort, direction);
    });
}

// View
<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<MyProject.Controllers.MyController.MyViewModel>" %>
...

<%= Html.ActionLink("Next", "List", new { page = Model.CurrentPage + 1, sort = "Title", direction = "ASC" }) %>

<% foreach (SomeObject obj in Model.Items) { %>
    <!-- Display each item -->
<% } %>
0
source

Source: https://habr.com/ru/post/1756772/


All Articles