2014-02-27 24 views
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); 
} 

回答

0

當你提交你沒有得到的形式從動作返回的消息。您可以將想要顯示的消息保存在某個會話變量中,並檢查是否該消息e會話變量包含任何值或不在文檔就緒的頁面中並相應地顯示消息。