javascript - Bootstrap Modal,在单击按钮时调用 Action Result 方法

标签 javascript jquery asp.net-mvc twitter-bootstrap modal-dialog

我需要帮助。

在我看来,我有一个文件选择器,它将在我的数据库中的某个表中加载一个文件,单击按钮时它会调用一个 Bootstrap 模式来询问用户是否要上传更多文件。

我的看法:

 @using (Html.BeginForm("UploadFile", "Upload", FormMethod.Post, new { enctype = "multipart/form-data" }))
            {

            <div>
                @*@Html.TextBox("file", "", new { type = "file" }) <br />*@
                <input type="file" name="file" accept=".csv" />
                @*<input type="submit" value="Upload New Accounts File" class="btn btn-primary btn-lg" onclick="Confirm()" />*@
                <input type="submit" value="Upload New Accounts File" class="btn btn-primary btn-lg"  data-toggle="modal" data-target="#confirmDialog" />
                @ViewBag.Message
            </div>
        }

我的模式:

<!-- Modal -->
<div class="modal fade" id="confirmDialog" tabindex="-1" role="dialog" aria-labelledby="ModalLabel" aria-hidden="true">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title" id="ModalLabel">Confirmation Required</h5>
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                </button>
            </div>
            <div class="modal-body">
                Do you have more files to upload?
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-primary" data-dismiss="modal">Yes</button>
                <button type="button" class="btn btn-secondary" data-dismiss="modal">No</button>
            </div>
        </div>
    </div>
</div>

我的 Controller :

public ActionResult UploadFile()
        {
            return View();
        }
        [HttpPost]
        public ActionResult UploadFile(HttpPostedFileBase file, string confirm_value)
        {
//TODO
//TODO
}

我的问题如下:

在设置模态之前,当我单击“上传新帐户文件”这个按钮时,文件会加载到数据库中,但现在它会调用模态。当用户在模式上单击"is"时,我想将文件加载到数据库中,但我不知道如何加载。

请帮忙。

最佳答案

您可以使用 Ajax如下所示:

模态(添加点击按钮)

<div class="modal-footer">
    <button type="button" onclick="upload()" class="btn btn-primary" data-dismiss="modal">Yes</button>
     ...
</div>

JS

function upload() {
    var fdata = new FormData();

    $('input[name="file"]').each(function(a, b) {
        var fileInput = $('input[name="file"]')[a];
        if (fileInput.files.length > 0) {
            var file = fileInput.files[0];
            fdata.append("fileUpload", file);
        }
    });


    $.ajax({
        type: 'post',
        url: '/UploadFile/Upload',
        data: fdata,
        processData: false,
        contentType: false,
        success: function(e) {
            // Do success process
        }
    });
}

Action

[HttpPost]
public ActionResult UploadFile(HttpPostedFileBase file) {
    //TODO
    //TODO
}

关于javascript - Bootstrap Modal,在单击按钮时调用 Action Result 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50083125/

相关文章:

javascript - 如何标记化一次,在 riak key 过滤器中重用标记

java - 如何使用struts taglib将参数传递给javascript函数?

javascript - jquery如何判断我点击的td是第一行还是最后一行,以及行索引和列索引?

javascript - 如何在带有闭包的 PrimeFaces 中的命令按钮上定义 oncomplete 事件?

javascript - JS 检测空结果

javascript - 使用 Angular 与 Node.js 获取图像尺寸

Javascript - 过滤包含数组的对象,其值来自另一个数组

Jquery选择除特定类别之外的所有图像

c# - HttpBasicAuthenticator 身份验证问题

asp.net-mvc - 数据库密码存储在 Web Deploy pubxml 文件中