Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8518563
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T06:00:49+00:00 2026-06-11T06:00:49+00:00

I have set up a webgrid and it seems to work fine, allowing me

  • 0

I have set up a webgrid and it seems to work fine, allowing me to sort and page. I have added a filter option which also works well however if I filter and then sort the results, the filter is lost and all records are displayed.

Here is my Razor view code:

@using (Ajax.BeginForm(new AjaxOptions { HttpMethod = "Get", InsertionMode = InsertionMode.Replace, UpdateTargetId = "myGrid" }))
{

@Html.ValidationSummary(true)
<fieldset>
    <legend>Document Search</legend>

    <div class="editor-label">
        @Html.Label("Enter a Document code:")
    </div>
    <div class="editor-field">
        @Html.Editor("search")
    </div>

    <p>
        <input type="submit" value="Search" />
    </p>
</fieldset>
}

@{
    WebGrid grid = new WebGrid(null, rowsPerPage: 10, canPage: true, canSort: true, ajaxUpdateContainerId: "myGrid");
    grid.Bind(Model, autoSortAndPage: true);
}

<div id="myGrid">

    @grid.GetHtml(mode: WebGridPagerModes.All, firstText: "First Page", nextText: "Next", previousText: "Previous", lastText: "Last Page", numericLinksCount: 10,
            columns: grid.Columns(
                grid.Column("DocumentID", "Document Code", canSort: true),
                grid.Column("Title", "Document Title", canSort: true)
        )
    )

</div>

And here is my action:

public ActionResult Index(string search)
    {
        List<DocumentIndexViewModel> viewModel = Mapper.Map<List<DocumentIndexViewModel>>(DocumentService.GetDocumentsBySearch(search));
        if (Request.IsAjaxRequest())
            return PartialView("_IndexGrid", viewModel);
        else
            return View(viewModel);
    }

How do I maintain the filter when I sort the displayed records? It seems like I need to append the search string onto the sort links somehow but am not sure how to proceed.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T06:00:51+00:00Added an answer on June 11, 2026 at 6:00 am

    Since you are using GET for the filter this should preserve it. I am unable to reproduce the problem. Here’s my full working test case.

    Model:

    public class CityViewModel
    {
        public int Id { get; set; }
        public string Name { get; set; }
    }
    

    Controller:

    public class HomeController : Controller
    {
        public ActionResult Index(string search)
        {
            using (var client = new WebClient())
            {
                var query = HttpUtility.ParseQueryString(string.Empty);
                query["q"] = search;
                var json = client.DownloadString("http://gd.geobytes.com/AutoCompleteCity?" + query.ToString());
                var serializer = new JavaScriptSerializer();
                var viewModel = serializer
                    .Deserialize<string[]>(json)
                    .Select((x, index) => new CityViewModel
                    {
                        Id = index,
                        Name = x
                    })
                    .Where(x => x.Name.StartsWith(search ?? string.Empty, StringComparison.OrdinalIgnoreCase))
                    .ToList();
    
                if (Request.IsAjaxRequest())
                {
                    return PartialView("_IndexGrid", viewModel);
                }
                else
                {
                    return View(viewModel);
                }
            }
        }
    }
    

    Main view (~/Views/Home/Index.cshtml):

    @model IEnumerable<CityViewModel>
    
    <script src="@Url.Content("~/Scripts/jquery.unobtrusive-ajax.js")" type="text/javascript"></script>
    <script type="text/javascript">
        $.ajaxSetup({
            cache: false
        });
    </script>
    
    @using (Ajax.BeginForm(new AjaxOptions { HttpMethod = "GET", InsertionMode = InsertionMode.Replace, UpdateTargetId = "gridPartial" }))
    {
        @Html.ValidationSummary(true)
        <fieldset>
            <legend>Document Search</legend>
    
            <div class="editor-label">
                @Html.Label("Enter a Document code:")
            </div>
            <div class="editor-field">
                @Html.Editor("search")
            </div>
    
            <p>
                <input type="submit" value="Search" />
            </p>
        </fieldset>
    }
    
    <div id="gridPartial">
        @Html.Partial("_IndexGrid")
    </div>
    

    ~/Views/Home/_IndexGrid.cshtml partial:

    @model IEnumerable<CityViewModel>
    
    @{
        WebGrid grid = new WebGrid(null, rowsPerPage: 10, canPage: true, canSort: true, ajaxUpdateContainerId: "myGrid");
        grid.Bind(Model, autoSortAndPage: true);
    }
    
    <div id="myGrid">
        @grid.GetHtml(mode: WebGridPagerModes.All, firstText: "First Page", nextText: "Next", previousText: "Previous", lastText: "Last Page", numericLinksCount: 10,
            columns: grid.Columns(
                grid.Column("Id", "City Id", canSort: true),
                grid.Column("Name", "City Name", canSort: true)
            )
        )
    </div>
    

    Sorting and pagination preserves the search filter that was entered because it was in the query string.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have set a background on the data-role=page element like so <div data-role=page style=background:
I have set default property page so in the property url it shows /lima-investments/property-name.
I have set up a macro for Smarty in Komodo Edit which adds a
I have set of classes which inherit from a single super class: Super |
I have set up VisualSvn Server, created a repository and added Visual Studio solution
I have set up Tapestry 5 project and all went fine, until I deployed
I have set up TinyMCE to work with the Admin panel (as per the
I have set the receiveBufferSize option to 1024, but for some reason I'm still
I have set of categories (the number of which changes according to specific state)
I have set the eclipse java formatter to wrap lines that exceed 120 characters

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.