mvc ajax 表单提交表单,MVC与Ajax和jQuery表单提交问题

我正在使用MVC 4并提交一个我已经验证过的Ajax表单,它正在成功更新模型数据库。我向用户显示了一个jQuery对话框,用户可以编辑对话框中的表单字段,然后更新或取消。

一切正常,直到我更新表格。我没有关闭jQuery对话框,而是使用来自Controller的Json返回值转储到空白页面上。

有什么想法吗?

查看:

@using (Ajax.BeginForm("Edit", "References", new AjaxOptions

{

InsertionMode = InsertionMode.Replace,

HttpMethod = "POST",

OnSuccess = "updateSuccess"

}, new { @id = "updateCarForm" }))

{

//etcetera

控制器:

public ActionResult Edit(int? id)

{

if (id == null)

{

return new HttpStatusCodeResult(HttpStatusCode.BadRequest);

}

ReferencesData referencesData = db.ReferencesDatas.Find(id);

if (referencesData == null)

{

return HttpNotFound();

}

return PartialView(referencesData);

}

[HttpPost]

[ValidateAntiForgeryToken]

public ActionResult Edit([Bind(Include = "make, model, colour")] ReferencesData referencesData)

{

if (ModelState.IsValid)

{

db.Entry(referencesData).State = EntityState.Modified;

db.SaveChanges();

return Json(JsonResponseFactory.SuccessResponse("Woohoo"), JsonRequestBehavior.DenyGet);

}

else

{

return Json(JsonResponseFactory.ErrorResponse("Please review your form"), JsonRequestBehavior.DenyGet);

}

}

使用Javascript:

var linkObj;

$(function () {

$(".editLink").click(function () {

//change the title of the dialog

linkObj = $(this);

var opt = {

autoOpen: false,

width: 1145,

height: 600,

resizable: false,

modal: true,

title: 'Edit Reference',

buttons: {

"Update": function () {

$("#update-message").html(''); //make sure there is nothing on the message before we continue

$("#updateCarForm").submit();

},

"Cancel": function () {

$(this).dialog("close");

}

}

};

var dialogDiv = $("#updateDialog").dialog(opt);

var viewUrl = linkObj.attr('href');

$.get(viewUrl, function (data) {

dialogDiv.html(data);

//validation

var $form = $("#updateCarForm");

// Unbind existing validation

$form.unbind();

$form.data("validator", null);

// Check document for changes

$.validator.unobtrusive.parse(document);

// Re add validation with changes

$form.validate($form.data("unobtrusiveValidation").options);

//open dialog

dialogDiv.dialog('open');

});

return false;

});

});

function updateSuccess(data) {

if (data.Success == true) {

//we update the table's info

var parent = linkObj.closest("tr");

parent.find(".carName").html(data.Object.Name);

parent.find(".carDescription").html(data.Object.Description);

//now we can close the dialog

$('#updateDialog').dialog('close');

//twitter type notification

$('#commonMessage').html("Update Complete");

$('#commonMessage').delay(400).slideDown(400).delay(3000).slideUp(400);

}

else {

$("#update-message").html(data.ErrorMessage);

$("#update-message").show();

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值