【发布时间】:2014-02-27 11:32:10
【问题描述】:
发布表单后如何在 jquery 对话框中显示来自“return json(message)”操作的消息。我尝试了以下,一切正常,但返回 JsonResult 触发保存/打开提示,而不是使用 Ajax.BeginForm 调用 OnSuccess。
局部视图:
@using (Ajax.BeginForm("SaveDetails", "FileManage", new AjaxOptions { HttpMethod = "POST", OnSuccess = "OnFileUploadSuccess" }, new { enctype = "multipart/form-data", id = "myForm" }))
{
<input id="fuMyFile" type="file" name="files" />
<input id="btnSubmit" type="submit" value="Submit" />
}
<div id="dialogboxWin" style="display: none; padding: 8px 15px;">
<div id="dvWindow"></div>
</div>
以下是jQuery代码:
$('#btnSubmit').click(function () {
$("#dvWindow").html("Are you sure to submit?");
$("#dialogboxWin").dialog({
modal: true,
width: 400,
autoOpen: true,
title: 'Confirmation',
buttons: {
"Yes": function () {
$(this).dialog("close");
$('#myForm).submit();
},
"No": function () {
$(this).dialog("close");
}
}
});
return false;
});
function OnFileUploadSuccess(data) {
alert(data.Message);
}
控制器动作方法:
[HttpPost]
public JsonResult SaveDetails(HttpPostedFileBase file)
{
bool isSaved = File Saving & Some DB operations
return Json(new
{
Result = isSaved
Message = (isSaved)?"Saved Successfully." : "Failed"
}, JsonRequestBehavior.AllowGet);
}
【问题讨论】:
标签: c# jquery asp.net-mvc