I'm using MVC 4 and submitting an Ajax form which I have verified is updating the model database sucessfully. I'm displaying a jQuery dialog to the user, who can edit the form fields within the dialog and then update or cancel.
我正在使用MVC 4并提交一个我已经验证过的Ajax表单正在成功地更新模型数据库。我向用户显示一个jQuery对话框,用户可以在对话框中编辑表单字段,然后更新或取消。
Everything works fine, until I update the form. Instead of closing the jQuery dialog, I'm dumped onto a blank page with the Json return value from my Controller. Any ideas?
一切正常,直到我更新表单。我没有关闭jQuery对话框,而是使用来自控制器的Json返回值转储到一个空白页面。什么好主意吗?
View:
观点:
@using (Ajax.BeginForm("Edit", "References", new AjaxOptions
{
InsertionMode = InsertionMode.Replace,
HttpMethod = "POST",
OnSuccess = "updateSuccess"
}, new { @id = "updateCarForm" }))
{
//etcetera
Controller:
控制器:
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:
Javascript:
<div id="updateDialog" title="Update Car"></div>
<script type="text/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();
}
}
</script>
2 个解决方案
#1
1
You need to catch the submit in JS.
您需要用JS捕获提交。
$('form').submit(function(){
//serialize form
$.post(url,{formData}function(json){
//do what you want with the json
})
})
The way you have it will change the view because when you do a plain form submit it wants to change its view with what ever result it gets.
它的方式将会改变视图,因为当你做一个简单的表单提交时,它想要改变它的视图和它得到的结果。
#2
0
Have you enabled Unobtrusivejavascript in your web.config
在web.config中是否启用了Unobtrusivejavascript
<add key="UnobtrusiveJavaScriptEnabled" value="true" />
Needless to say that you have to include
不用说,你必须包括在内
jquery.unobtrusive-ajax.js
#1
1
You need to catch the submit in JS.
您需要用JS捕获提交。
$('form').submit(function(){
//serialize form
$.post(url,{formData}function(json){
//do what you want with the json
})
})
The way you have it will change the view because when you do a plain form submit it wants to change its view with what ever result it gets.
它的方式将会改变视图,因为当你做一个简单的表单提交时,它想要改变它的视图和它得到的结果。
#2
0
Have you enabled Unobtrusivejavascript in your web.config
在web.config中是否启用了Unobtrusivejavascript
<add key="UnobtrusiveJavaScriptEnabled" value="true" />
Needless to say that you have to include
不用说,你必须包括在内
jquery.unobtrusive-ajax.js