我在我的WebGrid删除一行,并删除后重定向到我的GET指标正常。然而,由于预期我的看法是显出老删除的行仍然在网格中。我知道这是由设计,我应该能够单独设置的ModelState清除,或删除项目来实现这一点:
ModelState.Remove(ID);或者
的foreach(在ModelState.Keys.Where(M = GT VAR键。m.StartsWith(ID))了ToList()) ModelState.Remove(键);或者
ModelState.Remove(Applicant.ApplicantId);甚至
ModelState.Clear()但没有对我的工作。
下面是我的删除方法(简单 - 所有的错误处理和非必要的code删除)
公众的ActionResult DeleteApplicant(INT ID) { 变种Q = (来自于context.Applicants 其中,a.ApplicantId == ID 选择一个)。单<申请人GT;(); 。q.ApplicantDocuments.ToList()的ForEach(R => context.ApplicantDocuments.Remove(R)); context.Applicants.Remove(Q); context.SaveChanges(); 的foreach(; m.StartsWith(ID)))了ToList(在ModelState.Keys.Where(M = GT VAR键)。 ModelState.Remove(键); TempData的[SuccessMessage] =已成功删除的行。 返回RedirectToAction(「指数」); }这是我的电话,从我的观点删除方法:
,grid.Column(格式:(项目)=> @ Html.ActionLink(删除,DeleteApplicant 新{ID = item.ApplicantId},{新@class =删除链接}),风格:DeleteButton)我已经看了看计算器等,但没有各个岗位似乎解决我的问题:MVC 3模型提交后视图没有被刷新
我曾尝试也试图重新定向到通过jQuery Index操作和调用带有ajaxOptions删除控制器动作,但是这并没有任何工作。
,grid.Column(格式:(项目)=> @ Ajax.ActionLink(删除,DeleteApplicant 新{ID = item.ApplicantId}, 新AjaxOptions {列举HTTPMethod =GET,的onSuccess =reloadGrid}))和加入少许脚本调用入户指标:
函数reloadGrid(){ VAR pathArray = window.location.pathname.split('/'); 变种segment_1 = pathArray [1]; VAR的newURL = window.location.protocol +//+ window.location.host +/+ segment_1 +/首页/索引; $阿贾克斯( { 键入:GET, 网址:的newURL, 数据:{}, 缓存:假的, 数据类型:HTML, 成功:功能(数据) {$()的HTML(数据)。 } })}我明明做错了什么。有没有其他办法可以强制页面的刷新以不同的方式或任何人能发现什么明显的错误在我目前的做法。先谢谢了。
请注意:我有我的索引视图,而不是在一个单独的部分viewbut我不认为这应该是造成这是我的WebGrid标记
更新的
下面是高达GET方法的要求:(上下文,过滤器被点击搜索按钮,页面被调回,使用对象中没有在初始页面加载应用的过滤器)
公众的ActionResult指数(串页) { ///// [执行寻呼开始 INT的PageIndex = 0; 如果(!string.IsNullOrEmpty(页)) { 的PageIndex = int.Parse(页) - 1; } ////初始化记录总数和过滤结果对象 INT totalRecordCount = 0; VAR过滤器=新ApplicantSearchFilter(); ////执行分页搜索 VAR申请人= this.GetApplicantsPaging(过滤器,PageIndex的,出totalRecordCount); ////返回视图类型 VAR数据=新ApplicantViewModel() { 过滤器=过滤器, TotalRecordCount = totalRecordCount, ApplicantReportLists =申请人, 的PageIndex = PageIndex的, }; ////返回索引视图传递数据的结果来构建它 返回this.View(索引,数据); } }解决方案
我终于设法得到这个工作。
如果它可以帮助别人这是我最后还是没买。我曾经发表德罗尔这里的解决方案:How实现编辑和删除MVC3剃刀?的的WebGrid。他在那里转了GET删除通话距离的WebGrid到一个职位。不正是我一直在寻找,但它的工作原理。
有没有人有一个更好的,可替代的解决方案?
查看code:
补充功能:
@functions { 串删除(动态P) { 字符串的ActionController = Url.Action(删除,管理,新{ID = p.AccountId}); 回归<形式风格=显示:内联;'方法='后'行动='+的ActionController +'><输入类型=提交值=删除的onclick = \\返回确认(你确定吗?')\\/>< /表格及GT; }}和改变了我删除调用在我的WebGrid为:
grid.Column(标题:,格式为:P => Html.Raw(删除(P)))在控制器:
[HttpPost]公众的ActionResult删除(INT ID){ PerformDelete(ID); 返回RedirectToAction(「指数」);}I am deleting a row in my webgrid , and redirecting to my GET index as normal after delete. However as expected my view is showing the old deleted row still in the grid. I know this is by design and i should be able to set the Modelstate to clear , or remove the item individually to achieve this :
ModelState.Remove("id");Or
foreach (var key in ModelState.Keys.Where(m => m.StartsWith("id")).ToList()) ModelState.Remove(key);Or
ModelState.Remove("Applicant.ApplicantId");Or even :
ModelState.Clear()However none have working for me.
Here is my delete method (simplified -all error handling and non necessary code removed )
public ActionResult DeleteApplicant(int id) { var q = (from a in context.Applicants where a.ApplicantId == id select a).Single<Applicant>(); q.ApplicantDocuments.ToList().ForEach(r => context.ApplicantDocuments.Remove(r)); context.Applicants.Remove(q); context.SaveChanges(); foreach (var key in ModelState.Keys.Where(m => m.StartsWith("id")).ToList()) ModelState.Remove(key); TempData["SuccessMessage"] = "Successfully deleted row."; return RedirectToAction("Index"); }And here is my call to my delete method from the view :
, grid.Column(format: (item) => @Html.ActionLink("Delete", "DeleteApplicant", new { id = item.ApplicantId }, new { @class = "delete-link" }), style: "DeleteButton")I have looked at various posts on stackoverflow etc but none seem to solve my issue : MVC 3 The View is not being refreshed after model submit
I have tried also tried re-directing to the Index action via jquery and calling the delete controller action with ajaxOptions , but this hasn't worked either.
, grid.Column(format: (item) => @Ajax.ActionLink("Delete", "DeleteApplicant", new { id = item.ApplicantId }, new AjaxOptions { HttpMethod = "GET" , OnSuccess= "reloadGrid" }))And adding a little script to call the home index:
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); } }) }I am obviously doing something wrong. Is there any other way i can force a refresh of the page in a different way or can anyone spot anything obviously wrong in my current approach. Thanks in advance.
Note: I do have my webgrid markup on my Index view and not in a separate partial viewbut i don't think this should be causing this?
Update :
Here is up GET method as requested: (for context, the filters object is used when search button is clicked and page is posted back ,no filters applied in initial page load)
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); } }解决方案
I have finally managed to get this working.
If it helps anyone else here is what i did in the end. I used a solution posted by Dror here : How to achieve edit and delete on Webgrid of MVC3 Razor? . Where he turned a GET delete call from the webgrid into a post. Not exactly what i was looking for but it works.
Does anyone have a better , alternative solution ?
View Code :
added Function :
@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>"; } }and changed my delete call in my webgrid to :
grid.Column(header: "", format: p => Html.Raw(Delete(p)))In the Controller:
[HttpPost] public ActionResult Delete(int id) { PerformDelete(id); return RedirectToAction("Index"); }
更多推荐
的WebGrid不刷新后删除MVC
发布评论