$("button[name='advocate-create-button']").on("click", function () {
$.ajax({
url: '/Vendor/AdvocateCreate',
type: 'post',
dataType: 'json',
data: $('form#advocate-create-form').serialize(),
success: function() {
$(".modal_overlay").css("opacity", "0");
$(".modal_container").css("display", "none");
}
}).done(function (data) {
var $target = $("#advocate-list-view");
var $newHtml = $(data);
$target.replaceWith(data);
$newHtml.effect("highlight");
});
return false;
});
几乎做到了,只需要一点帮助就可以完成...我正在尝试将表单数据发布到“/Vendor/AdvocateCreate”,一旦保存,我希望对话消失并且它背后的列表是更新。
它后面的列表是 AdvocateList 视图,并从同一控制器中的 AdvocateList 方法中提取其数据
AdvocateCreate 方法
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult AdvocateCreate(Advocate advocate, int page = 1)
{
if (!ModelState.IsValid) return View(advocate);
db.Advocates.Add(advocate);
db.SaveChanges();
var userId = WebSecurity.GetUserId(User.Identity.Name);
var model = (from a in db.Advocates.ToList()
where a.VendorId == userId
select a).ToPagedList(page, 15);
if (Request.IsAjaxRequest()) return PartialView("_AdvocateListPartial", model);
return View(model);
}
因此,表单标签是:<form class="form" id="advocate-create-form">
确实调用了 create 方法,确实保存了数据,但是成功下的行:不要触发并且#advocate-list-view 中的数据未更新
谢谢