我再次获取GET时清除以前的GET参数(查询字符串参数)

时间:2011-06-02 15:57:51

标签: c# asp.net-mvc-3

我正在使用MVC3,C#,Razor。我有一个视图(Index.cshtml),其中包括对Partial(List.cshtml)的引用。页面上还有一个Ajax表单(Ajax.BeginForm ...),它是列表的过滤器。我一直在使用Ajax表单的默认POST方法,一切都很好。但是,有一件事不能像我的客户所期望的那样起作用。当我过滤列表,然后单击一个项目以查看它的详细信息(Detail.cshtml),然后单击返回它不会显示已过滤的列表但返回到整个列表。因此,我想将我的表单方法更改为GET以解决此问题(以及让我能够将人员直接链接到已过滤的列表)。我这样做但它引入了一个新的异常。我过滤我的列表,然后选择一个项目的详细信息,然后单击返回。现在我得到了我的过滤列表(根据需要),但我也得到了URL中的查询字符串,该字符串覆盖了后续尝试过滤列表的任何内容。我不确定是否需要手动清除查询字符串或者是否需要完全追求新模式。这是相关的代码:


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 - 索引操作

        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);
    }

提前致谢。

1 个答案:

答案 0 :(得分:0)

是否考虑过使用单独的机制来维护列表/排序的状态?也许是通过JavaScript维护和加载的cookie,或者可能是通过隐藏字段包含“控件”状态的自定义机制?