ONCLICK存储相同的值多次 [英] Onclick storing same value multiple times

查看:109
本文介绍了ONCLICK存储相同的值多次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑以下code段

[HttpPost]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Create(MyViewModel viewModel)
{
        if (ModelState.IsValid)
        {
            //map properties here

            using (var context = new MyEntities())
            {
                context.Users.Add(user);
                context.SaveChanges();
            }

            if (Request.Files.Count > 0)
            {
                foreach (string fileName in Request.Files)
                {
                    HttpPostedFileBase file = Request.Files[fileName];
                    if (file != null && file.ContentLength > 0)
                    {
                       //do checks and upload file here
                    }
                }
            }
        }
        return RedirectToAction("Index", "Home");
}

该表格可以提交作为一个独立的或,然后上载到服务器的文件。现在我的问题是,如果我提交表单没有任何文件,或只是一个文件,然后一切正常。但是用户可以一次上传多个文件而这也正是问题的用武之地。这些文件上载到但我得到了那个特定的形式在数据库中的多个条目。例如,如果用户上传的三个文件,我会在数据库中保存三个条目正是相同。

The form can be submitted as a standalone or with files which then get uploaded to a server. Now my issue is If I submit the form without any files or just one file then everything works as expected. However users can upload more than one file at a time and that's where the problem comes in. The files get uploaded but I get more than one entry in the database for that particular form. For example if the user uploads three files I'll get three entries in the database exactly that same.

所以我的问题是我怎么解决这个得到什么?

So my question is how do I get around this?

在客户端我使用 DropZoneJs 并调用方法

On the client side I'm using DropZoneJs and calling the method as

<script>
    Dropzone.autoDiscover = false;
    var myDropZone = new Dropzone("#dzUpload", {
        url: "/Home/Create",
        autoProcessQueue: false,
        previewsContainer: ".preview",
    });

    $("#submit-all").attr("type", "button").on('click', function (e) {
        e.preventDefault();
        e.stopPropagation();

        if (myDropZone.getQueuedFiles().length > 0) {
            myDropZone.options.autoProcessQueue = true;
            myDropZone.processQueue();
        }
        else {
            $("#dzUpload").submit();
        }
    });
</script>

我也遇到 <一个href=\"http://stackoverflow.com/questions/28021984/upload-multiple-files-in-one-request-dropzone-sending-two-requests?rq=1\">this的问题,但我仍然有同样的问题。

I've also come across this question but I still have the same issue

推荐答案

它看起来像 uploadMultiple 所以只有一个请求被发送到服务器选项会改变行为

It looks like the uploadMultiple option will change the behavior so only one request is sent to the server.

var myDropZone = new Dropzone("#dzUpload", {
    url: "/Home/Create",
    autoProcessQueue: false,
    previewsContainer: ".preview",
    uploadMultiple: true,
});

这篇关于ONCLICK存储相同的值多次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆