在使用ASP.NET MVC的AjaxHelper遇到问题,没有弹出代码中的确认alert对话框和审核通过alert对话框。麻烦走过路过的大神帮忙诊断一下,谢谢。
1.视图代码:
@model IEnumerable<GuestbookSystem.Models.Guestbook> @{ ViewBag.Title = "ListNotPassed"; Layout = null; } @{ var ajaxOptions1 = new AjaxOptions() { OnSuccess = "SetPassSuccess", OnFailure = "SetPassFailure", Confirm = "设置留言审核状态为'通过'?", HttpMethod = "Post" }; var ajaxOptions2 = new AjaxOptions() { OnSuccess = "DeleteSuccess", OnFailure = "DeleteFailure", Confirm = "确认删除留言?", HttpMethod = "Post" }; } @section scripts { @Scripts.Render("~/bundles/jqueryval") <script type="text/javascript"> function SetPassSuccess() { alert('审核通过'); location.reload(); } function SetPassFailure(xhr) { alert('审核失败(HTTP状态代码:' + xhr.status + ')'); } function SetPassSuccess() { alert('已成功删除'); location.reload(); } function SetPassFailure(xhr) { alert('删除失败(HTTP状态代码:' + xhr.status + ')'); } </script> } <h2>ListNotPassed</h2> @foreach (var item in Model) { <div style="clear:both;"> <p> @Html.DisplayNameFor(model => model.User.Name):@Html.DisplayFor(modelItem => item.User.Name), @Html.DisplayNameFor(model => model.User.Email):@Html.DisplayFor(modelItem => item.User.Email), @Html.DisplayNameFor(model => model.CreatedOn):@Html.DisplayFor(modelItem => item.CreatedOn) </p> <p> @Html.DisplayNameFor(model => model.Message):@Html.DisplayFor(modelItem => item.Message) </p> <p> <span> @Ajax.ActionLink("审核通过", "ShenHeGuestbook",new{id=item.GuestbookId}, ajaxOptions1) </span> </p> <hr /> </div> }
2.控制器代码
public ActionResult ShenHeGuestbook(int id) { var gu = db.Guestbooks.Find(id); gu.shenhe = true; db.SaveChanges(); return new HttpStatusCodeResult(System.Net.HttpStatusCode.OK); }
ps:ajax需要的两个js代码,一个在模板的@Scripts.Render("~/bundles/jquery")中导入,一个在视图的@Scripts.Render("~/bundles/jqueryval")中导入。
这里边有个错误,var ajaxOptions2 = new AjaxOptions()这个函数里边的method换成Delete,不过这个错误你没有用。
用F12 进行调试一下不就知道了