再次GET时清除以前的GET参数(查询字符串参数)
本文关键字:参数 GET 查询 字符串 清除 再次 | 更新日期: 2023-09-27 17:58:20
我使用的是MVC3、C#、Razor。我有一个视图(Index.cshtml),其中包含对Partial(List.cs.html)的引用。页面上还有一个Ajax表单(Ajax。BeginForm…),它是列表的过滤器。我一直在使用Ajax表单的默认POST方法,一切都很好。然而,有一件事并不像我的客户所期望的那样奏效。当我筛选列表时,单击某个项目以查看其详细信息(Detail.cs.html),然后单击返回,它不会显示筛选的列表,而是返回到整个列表。因此,我想将我的表单方法更改为GET来解决这个问题(并使我能够将一个人直接链接到筛选列表)。我这样做,但它引入了一种新的反常现象。我过滤我的列表,然后选择一个项目作为详细信息,然后单击返回。现在我得到了我的过滤列表(根据需要),但我也得到了URL中的查询字符串,它会覆盖任何后续的过滤列表的尝试。我不确定是否需要手动清除查询字符串,或者是否需要完全采用新的模式。以下是相关代码:
索引.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);
}
提前谢谢。
是否考虑使用单独的机制来维护列表/排序的状态?也许是通过JavaScript维护和加载的cookie,或者可能是通过隐藏字段包含"控件"状态的自定义机制?