Asp.net mvc 删除MVC后Webgrid未刷新
我正在删除webgrid中的一行,并在删除后像往常一样重定向到GET索引。 但是,正如预期的那样,我的视图显示的是仍在网格中的旧删除行。我知道这是出于设计,我应该能够将Modelstate设置为clear,或者单独删除该项以实现这一点:Asp.net mvc 删除MVC后Webgrid未刷新,asp.net-mvc,razor,entity-framework-5,webgrid,Asp.net Mvc,Razor,Entity Framework 5,Webgrid,我正在删除webgrid中的一行,并在删除后像往常一样重定向到GET索引。 但是,正如预期的那样,我的视图显示的是仍在网格中的旧删除行。我知道这是出于设计,我应该能够将Modelstate设置为clear,或者单独删除该项以实现这一点: ModelState.Remove("id"); 或 或 甚至: ModelState.Clear() 然而,没有人为我工作 这是我的删除方法(简化-删除所有错误处理和不必要的代码) 我看过stackoverflow等的各种帖子,但似乎没有一篇能解决我的问
ModelState.Remove("id");
或
或
甚至:
ModelState.Clear()
然而,没有人为我工作
这是我的删除方法(简化-删除所有错误处理和不必要的代码)
我看过stackoverflow等的各种帖子,但似乎没有一篇能解决我的问题:
我也尝试过通过jquery重新定向到Index操作,并使用ajaxOptions调用delete控制器操作,但这也没有奏效
, grid.Column(format: (item) => @Ajax.ActionLink("Delete", "DeleteApplicant",
new { id = item.ApplicantId },
new AjaxOptions { HttpMethod = "GET" , OnSuccess= "reloadGrid" }))
并添加一个小脚本来调用主索引:
function reloadGrid() {
var pathArray = window.location.pathname.split('/');
var segment_1 = pathArray[1];
var newURL = window.location.protocol + "//" + window.location.host + "/" + segment_1 + "/Home/Index";
$.ajax(
{
type: "GET",
url: newURL,
data: "{}",
cache: false,
dataType: "html",
success: function (data)
{ $().html(data); }
})
}
我显然做错了什么。是否有其他方法可以强制以不同的方式刷新页面,或者任何人都可以发现我当前方法中的任何明显错误。提前谢谢
注意:我确实在索引视图中有我的webgrid标记,而不是在单独的部分视图中,但我认为这不应该导致这种情况
更新:
下面是请求的up-GET方法:(对于上下文,当单击搜索按钮并发回页面时使用filters对象,在初始页面加载中没有应用过滤器)
我终于设法让它工作起来了 如果这对其他人有帮助,那就是我最后所做的。我在这里使用了Dror发布的解决方案:。他把webgrid上的GET delete调用变成了一篇帖子。不完全是我想要的,但它很有效 有人有更好的替代方案吗 查看代码: 新增功能:
@functions{
string Delete(dynamic p)
{
string actionController = Url.Action("Delete", "Admin", new {id=p.AccountId});
return "<form style='display:inline;' method='post' action='" + actionController + "'><input type='submit' value='Delete' onclick=\"return confirm('Are you sure?')\"/></form>";
}
}
在控制器中:
[HttpPost]
public ActionResult Delete(int id)
{
PerformDelete(id);
return RedirectToAction("Index");
}
你能分享你重定向到的GET方法吗?当然,它现在在上面抱歉,我昨天很忙,所以无法回复,但是如果你能看到你的GET方法接受一个字符串参数“page”,因此,当你将你的操作重定向到“Index”时,它不起作用(找不到这个名为Index的GET操作,没有参数),返回时需要传递这个字符串参数,类似于return RedirectToAction(“Index”,new{page=your_value_to_pass);嗨,Krunal,谢谢你回复我。它将在我的索引操作中进行。好的,我在那里有一个断点,所以它一定是其他东西。你尝试过从“Index”调试吗方法和设计器页面?如果是,它是否给出了任何错误?我还认为您的Index()操作已被缓存。我们遇到了相同的问题。我曾经设置OutputCache(Duration=0,VaryByParam=“*”)。您可以设置VaryByParam=“page”,因为您正在获取页面索引。嗨,Sravan,感谢您的提示!我尝试使用该属性装饰我的索引方法[OutputCache(Duration=0,VaryByParam=“page”)]并在重定向时传入页面参数,但我得到了相同的结果,即结果不刷新。
, grid.Column(format: (item) => @Ajax.ActionLink("Delete", "DeleteApplicant",
new { id = item.ApplicantId },
new AjaxOptions { HttpMethod = "GET" , OnSuccess= "reloadGrid" }))
function reloadGrid() {
var pathArray = window.location.pathname.split('/');
var segment_1 = pathArray[1];
var newURL = window.location.protocol + "//" + window.location.host + "/" + segment_1 + "/Home/Index";
$.ajax(
{
type: "GET",
url: newURL,
data: "{}",
cache: false,
dataType: "html",
success: function (data)
{ $().html(data); }
})
}
public ActionResult Index(string page)
{
/////Peform paging initiation
int pageIndex = 0;
if (!string.IsNullOrEmpty(page))
{
pageIndex = int.Parse(page) - 1;
}
////initialize total record count and filter results object
int totalRecordCount = 0;
var filters = new ApplicantSearchFilter();
////Perform search with paging
var applicants = this.GetApplicantsPaging(filters ,pageIndex, out totalRecordCount);
////Return view type
var data = new ApplicantViewModel()
{
Filters =filters ,
TotalRecordCount = totalRecordCount,
ApplicantReportLists = applicants,
PageIndex = pageIndex,
};
////return Index view passing data result to build it
return this.View("Index", data);
}
}
@functions{
string Delete(dynamic p)
{
string actionController = Url.Action("Delete", "Admin", new {id=p.AccountId});
return "<form style='display:inline;' method='post' action='" + actionController + "'><input type='submit' value='Delete' onclick=\"return confirm('Are you sure?')\"/></form>";
}
}
grid.Column(header: "", format: p => Html.Raw(Delete(p)))
[HttpPost]
public ActionResult Delete(int id)
{
PerformDelete(id);
return RedirectToAction("Index");
}