0
如何在發佈表單後顯示jQuery對話框中「返回json(消息)」動作的消息。我嘗試了以下,一切工作正常,但返回JsonResult觸發保存/打開提示,而不是與Ajax.BeginForm的OnSuccess調用。在Ajax.BeginForm上傳文件後顯示一條消息
管窺:
@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);
}