【问题标题】:Display a message after file upload in Ajax.BeginForm在 Ajax.BeginForm 中文件上传后显示一条消息
【发布时间】: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


    【解决方案1】:

    当您提交表单时,您不会收到操作返回的消息。您可以将要显示的消息保存在某个会话变量中,并检查会话变量是否包含任何值页面并相应地显示消息。

    【讨论】:

      猜你喜欢
      • 2017-11-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-04-17
      • 1970-01-01
      相关资源
      最近更新 更多