1
當我按保存按鈕,我從我的控制器返回此:jQuery的對話框
return Json(new { success = true });
和我的整個瀏覽器窗口爲白色,並在左上角我看到我的JSON我已經返回的數據:
{"success":true}
問題:我必須改變這個白色視口是否已修復?
我的期望是檢查是否成功:客戶端的真/假,取決於值我關閉對話框或更改一些數據,並且應該保留調用對話框的網站。
$(document).ready(function() {
// the div holds the html content
var $dialog = $('<div></div>')
.dialog({
autoOpen: false,
title: 'This is the dialogs title',
height: 400,
width: 400,
modal: true,
resizable: false,
hide: "fade",
show: "fade",
open: function (event, ui) {
$(this).load('@Url.Action("Create")');
},
buttons: {
"Save": function() {
var form = $('form', this);
$(form).submit();
},
"Close": function() {
$(this).dialog("close");
}
} // no comma
});
$('#CreateTemplate').click(function() {
$dialog.dialog('open');
// prevent the default action, e.g., following a link
return false;
});
});
</script>
創建視圖:
@using (Html.BeginForm("JsonCreate", "Template"))
{
<p class="editor-label">@Html.LabelFor(model => model.Name)</p>
<p class="editor-field">@Html.EditorFor(model => model.Name)</p>
<p class="editor-field">@Html.ValidationMessageFor(model => model.Name)</p>
}
UPDATE
"Save": function() {
var form = $('form', this);
debugger;
$.ajax({
url: this.action,
type: 'POST',
data: form.serialize(),
dataType: 'json',
success: function (result) {
debugger;
if (result.success) {
$(this).dialog("close");
// Update UI
}
else {
// Reload the dialog to show model errors
$(dialog).html(result);
} // else end
} // success end
}); // ajax post end
},
控制器 POST操作:
[HttpPost]
public ActionResult JsonCreate(Template template)
{
if (ModelState.IsValid)
{
_templateDataProvider.AddTemplate(template);
return Json(new { success = true });
}
return Json(new { errors = GetErrorsFromModelState() });
}
請你重寫你的$。員額代碼,因爲風格是不容易理解爲jQuery的初學者(我)。我想不知怎的,.success(可以重寫更多的行權?因爲你的ajax提交我想我使用然後$ .ajax({data:.. type:.. url:..}); – Elisabeth
現在它調用$ .post(url,data,callback,type),就像http://api.jquery.com/jQuery.post/ – deerchao
上的文檔我用新的ajax代碼更新了我的問題:你能告訴我爲什麼調試器當我到達$ .ajax調用併成功時停止==真正的我的對話框沒有關閉?有什麼東西在這裏不起作用... – Elisabeth