开发者

Clearing previous GET parameters (query string parameters) when I GET again

开发者 https://www.devze.com 2023-03-09 21:01 出处:网络
I\'m using MVC3, C#, Razor. I have a view (Index.cshtml) that includes a reference to a Partial (List.cshtml). There is also an Ajax form (Ajax.BeginForm...) on the page that is a filter for the list.

I'm using MVC3, C#, Razor. I have a view (Index.cshtml) that includes a reference to a Partial (List.cshtml). There is also an Ajax form (Ajax.BeginForm...) on the page that is a filter for the list. I've been using the default POST method for the Ajax form and everything works great. One thing doesn't work as my client expects, however. When I filter the list, then click on an item to view it's details (Detail.cshtml), and then click back it does not show me the filtered list but reverts to the entire list. So I would like to change my form method to a GET to fix this (as well as to give me the ability to link a person directly to a filtered list). I do this but it introduces a new anomoly. I filter my list, then choose an item for detail, then click back. Now I get my filtered list (as desired) but I also get the query string in the URL which overrides any subsequent attempts to filter the list. I'm not sure if I need to manually clear my query string or if I need to pursue a new schema altogether. Here's the pertinent code:


Index.cshtml

...

    @using (Ajax.BeginForm(new AjaxOptions { UpdateTargetId = "resultslist", LoadingElementId="loadingResults", OnComplete = "initializePage", HttpMethod="Get" }))
{ 
    <div class="pane">Filter</div>
    <div>
        <div class="filterfield">
            <div class="field">@Html.TextBox("keywords", "")</div>
            <div class="fieldname">Keywords</div>
        </div>
        <div class="filterfield">
            <div class="field">@Html.DropDownList("type", Model.Types, "")</div>
            <div class="fieldname">Type</div>
        </div>
        <div class="filterfield">
            <div class="field">@Html.DropDownList("category", Model.Categories, "")</div>
            <div class="fieldname">Category</div>
        </div>
        <div class="filterfield">
            <div class="field">@Html.DropDownList("subcategory", Model.Subcategories, "")</div>
            <div class="fieldname">Subcategory</div>
        </div>
        <div class="filterfield">
            <div class="field">@Html.Editor("capability","AutocompleteSingle", new { Id = "capability", Url = "/dp/api/pages/GetCapabilities" })</div>
            <div class="fieldname">Capability</div>
        </div>
        <input id="filterButton" type="submit" value="Filter" />
        <input type="button" onclick="$('form').clearForm();filterButton.click();" value="Clear" />
        <div style="clear:both;"></div>
    </div>
}
...
<div id="loadingResults" class="loading">
    loading...
</div>
<div id="resultslist">
    @Html.Partial("List", Model.Pages)
    @if(!ViewBag.Cached) {
        @:Building environments. Please wait. This may take a while.
    }
</div>

PagesController.cs - Index Action

        public ActionResult Index(string keywords, string category, string subcategory, string capability)
    {
        var pages = (CurrentEnvironment != null ? CurrentEnvironment.Pages.AsEnumerable() : new List<CustomPage>());

        //apply filters if they exist
        if (!string.IsNullOrEmpty(keywords))
            pages = pages.Where(
                page => page.Name.ToLower().Contains(keywords.ToLower()) ||
                    page.Category.ToLower().Contains(keywords.ToLower()) ||
                    page.Subcategory.ToLower().Contains(keywords.ToLower()) ||
                    page.Capabilities.Any(name => name.ToLower().Contains(keywords.ToLower())) ||
                    page.File.FullName.ToLower().Contains(keywords.ToLower()) ||
                    page.RevisionHistory.ToLower().Contains(keywords.ToLower()) ||
                    page.Summary.ToLower().Contains(keywords.ToLower())
            );
        if (!string.IsNullOrEmpty(category)) pages = pages.Where(p => p.Capabilities.Count > 0 && p.Category == category);
        if (!string.IsNullOrEmpty(subcategory)) pages = pages.Where(p => p.Capabilities.Count > 0 && p.Subcategory == subcategory);
        if (!string.IsNullOrEmpty(capability)) pages = pages.Where(p => p.Capabilities.Count > 0 && p.Capabilities.Any(c => c.ToLower().Contains(capability.ToLower())));

        //build a view model
        var result = new dp.ViewModels.PagesViewModel
        {
            Pages = pages.ToList(),
            Types = pages.Select(p => p.Type).Where(t => !string.IsNullOrWhiteSpace(t)).Distinct().OrderBy(t => t).ToSelectItemList(t => t, t => t),
            Categories = pages.Select(p => p.Category).Where(c => !string.IsNullOrWhiteSpace(c)).Distinct().OrderBy(c => c).ToSelectItemList(c => c, c => c),
            Subcategories = pages.Select(p => p.Subcategory).Where(s => !string.IsNullOrWhiteSpace(s)).Distinct().OrderBy(c => c).ToSelectItemList(s => s, s => s)
        };

        if (!Request.IsAjaxRequest())
            return View(result);
        else
            return PartialView("List", result.Pages);
    }

开发者_运维知识库Thanks in advance.


Have considered using a separate mechanism to maintain the state of the list/sorting? Maybe cookies that are maintained and loaded via JavaScript or potentially a custom mechanism for containing the state of the "control" via hidden fields?

0

精彩评论

暂无评论...
验证码 换一张
取 消