我有一个包含一些数据的网格。它看起来像这张照片。
当我选择要删除的几行并单击删除按钮时,在删除按钮中我对服务器端操作进行了 ajax 调用。服务器端操作从数据库中删除选定的类别,并将它们从它维护的内存缓存中删除。
然后,它重定向到一个动作,该动作返回具有类别网格的局部视图,并为其提供更新的模型。
但是,当我们在服务器端操作成功完成后看到网格时,它仍然保持旧的显示,即我们刚刚删除的行仍然作为选定行出现在网格中。
以下是相关的代码片段。
服务器端:
[HttpPost]
public ActionResult Delete(long[] categoryIds)
{
if (categoryIds != null && categoryIds.Length > 0)
{
foreach (var categoryId in categoryIds)
{
Category.Delete(categoryId);
DeleteFromCachedCategories(categoryId);
}
}
return PartialView("_CategoriesPartial", GetCategoryList());
}
private IEnumerable<Category> GetCategoryList()
{
lock (padLock)
{
if (CacheManager.Contains(CacheKeys.AllCategories))
{
return CacheManager.Get<IEnumerable<Category>>(CacheKeys.AllCategories);
}
}
var list = Category.All as List<Category>;
if (!list.Contains(Category.Empty)) list.Insert(0, Category.Empty);
lock (padLock)
{
CacheManager.Add(CacheKeys.AllCategories, list);
}
return list;
}
private void UpdateCachedCategories(Category category)
{
var categories = CacheManager.Get<List<Category>>(CacheKeys.AllCategories);
if (categories != null)
{
if (categories.AsEnumerable().Contains(category, Category.CategoryIdComparer))
{
var cachedCategory = categories.SingleOrDefault(c => c.Id == category.Id);
cachedCategory.Name = category.Name ?? string.Empty;
}
else
{
categories.Add(category);
}
}
}
private void DeleteFromCachedCategories(long categoryId)
{
var categories = CacheManager.Get<List<Category>>(CacheKeys.AllCategories);
if (categories != null)
{
var index = categories.FindIndex(c => c.Id == categoryId);
if (index >= 0)
categories.RemoveAt(index);
}
}
查看(剃刀):[_CategoriesPartial.cshtml]
@using System.Web.UI.WebControls;
@using System.Data;
@model IEnumerable<GlobalizationUI.BusinessObjects.Category>
@Html.DevExpress().GridView(settings =>
{
settings.Name = "gvCategories";
settings.CallbackRouteValues = new { Controller = "Category", Action = "CategoriesPartial" };
settings.Width = 1200;
settings.SettingsPager.Position = PagerPosition.TopAndBottom;
settings.SettingsPager.FirstPageButton.Visible = true;
settings.SettingsPager.LastPageButton.Visible = true;
settings.SettingsPager.PageSizeItemSettings.Visible = true;
settings.SettingsPager.PageSizeItemSettings.Items = new string[] { "10", "20", "50", "100", "200" };
settings.SettingsPager.PageSize = 50;
settings.Settings.ShowFilterRow = true;
settings.Settings.ShowFilterRowMenu = true;
settings.CommandColumn.Visible = true;
settings.CommandColumn.ClearFilterButton.Visible = true;
settings.CommandColumn.ShowSelectCheckbox = true;
settings.Settings.ShowHeaderFilterButton = true;
settings.KeyFieldName = "Id";
settings.Columns.Add("Name");
settings.SettingsEditing.AddNewRowRouteValues = new { Controller = "Category", Action = "CreateNew" };
settings.SettingsEditing.UpdateRowRouteValues = new { Controller = "Category", Action = "Edit" };
settings.SettingsEditing.Mode = GridViewEditingMode.Inline;
settings.CommandColumn.Visible = true;
settings.CommandColumn.NewButton.Visible = true;
settings.CommandColumn.EditButton.Visible = true;
settings.CommandColumn.UpdateButton.Visible = true;
}).Bind(Model).GetHtml()
容器视图中的相关片段:[Index.cshtml]
@model IEnumerable<GlobalizationUI.BusinessObjects.Category>
@Html.Partial("_CategoriesPartial", Model)
<script type = "text/javascript">
//<![CDATA[
$(document).ready(
function () {
WireHandlers();
}
);
function DeleteCategories(selectedCategoriesArray) {
if (selectedCategoriesArray.length == 0) return;
debugger;
var url = '/Category/Delete';
$.ajax(url,
{
cache: false, async: false, type: 'POST',
data: JSON.stringify({ categoryIds: selectedCategoriesArray }), dataType: 'json',
contentType: 'application/json', traditional: true,
error: OnError, success: OnSuccess
});
function OnSuccess(data, textStatus, jqXHR) {
debugger;
}
function OnError(jqXHR, textStatus, errorThrown) {
}
}
function DeleteSelectedCategories() {
if (gvCategories.GetSelectedRowCount() == 0) return;
gvCategories.GetSelectedFieldValues('Id', DeleteCategories);
}
function WireHandlers() {
$('#btnDeleteCategory').unbind("click").click(DeleteSelectedCategories);
}
// ]]>
</script>
我在 Windows 7 Home Premium 64 位机器上使用适用于 ASP.NET MVC 4 的 DevExpress Extensions v12.2.10.0。