【发布时间】:2023-03-15 20:06:01
【问题描述】:
我有一个控制器,它有Add、Edit、Delete 和Index 操作方法。对于我的 Add 操作,在添加之后,它会重定向到 Index 以刷新网格以显示添加。
对于Delete,它不会重定向。正在删除,但只有在手动刷新页面时才能看到它。我试过了
return RedirectToAction(nameof(Index));
和
w("Index", otList);
其中“otList”是模型。
这是在控制器中调用删除操作的代码
$("#deleteButton").click(function () {
var button = $(this);
$.ajax({ method: 'POST', url: button.data('url') })
.done(function () {
$('#confirmModal').modal('hide');
button.removeData('url');
})
.fail(function () {
magalert.communicationError();
});
});
这是控制器中的Delete操作方法
public IActionResult Delete(int Id)
{
try
{
var jurisdictionId = _user.Current().JurisdictionId;
_electedOfficials.deleteOrgType(jurisdictionId, Id);
//Refresh data post delete for the grid
List<OrganizationType> otList = new List<OrganizationType>();
otList = (List<OrganizationType>)_electedOfficials.OrgTypeList(jurisdictionId);
//return View(otList);
//return RedirectToAction(nameof(Index));
return View("Index", otList);
}
catch (Exception e)
{
_logger?.LogCritical(new EventId(103, "CAdminOrganizationType"), e, $"Error when deleting id: " + Id);
throw;
}
}
任何想法为什么Index 视图不像Add 操作之后那样令人耳目一新?我尝试重定向并尝试在返回中指定视图和模型。
如果有帮助,这里是 Index 操作方法
public IActionResult Index()
{
try
{
var jurisdictionId = _user.Current().JurisdictionId;
List<OrganizationType> otList = new List<OrganizationType>();
otList = (List<OrganizationType>)_electedOfficials.OrgTypeList(jurisdictionId);
return View(otList);
}
catch (Exception e)
{
_logger?.LogCritical(new EventId(101, "CAdminOrganizationType"), e, $"Error when loading Organization Types View");
throw;
}
}
【问题讨论】:
-
您正在通过 AJAX 调用删除,但还希望屏幕重新加载?这没有多大意义。是要使用 AJAX,还是要重新加载页面?
-
我看不到这里使用 ajax 的原因。为什么不直接提交或引用控制器上的 Delete 方法?
标签: c# ajax asp.net-mvc