简体   繁体   English

如何在 Dropzone.js ASP.NET MVC C# 中传递 model 时传递图像

[英]How do I pass the images while passing a model in Dropzone.js ASP.NET MVC C#

To start, I'm making a product edit page.首先,我正在制作一个产品编辑页面。 When you edit a product, it's all the fields from a database going through a model. I'm using Dropzone.js to upload multiple images for the product with previews and an ability to remove before submitting the edits the user has made.当您编辑产品时,它是数据库中经过 model 的所有字段。我正在使用 Dropzone.js 为产品上传多张图片,并提供预览和在提交用户所做的编辑之前删除的功能。 A user can drag and drop or select multiple images.用户可以拖放或 select 多个图像。 This works fine, the problem is when trying to submit it, the images are not passed to the controller for some reason where as the model is.这工作正常,问题是在尝试提交时,由于某种原因,图像没有传递给 controller,而 model 是。 I made a page dedicated to the uploading and it works great, but when I attempt to pass a model and the images, it passes only the Model and the images are null. Controller我制作了一个专门用于上传的页面,效果很好,但是当我尝试传递 model 和图像时,它仅传递 Model,图像为null。Controller

[HttpPost]
    public ActionResult ProductEdit(IEnumerable<HttpPostedFileBase> files, TWebProduct tbl, HttpPostedFileBase file)
    {
        // A bunch of stuff that doesn't matter because it returns as null before it hits this.
    }

This is the top of the ProductEdit.cshtml as you can see the model and using tag.这是 ProductEdit.cshtml 的顶部,您可以看到 model 和 using 标签。 Top of the ProductEdit.cshtml ProductEdit.cshtml 的顶部

@model EcommerceAirmotion.DAL.TWebProduct
    
    @{
        ViewBag.Title = "ProductEdit";
        Layout = "~/Views/Shared/_AdminLayoutPage.cshtml";
    }
    
    <h2>Product Details</h2>
    
    <script src="~/Scripts/jquery-3.6.0.min.js"></script>
    @using (Html.BeginForm("ProductEdit", "Admin", FormMethod.Post, new { @name = "myDropzone", id = "myDropzone", enctype = "multipart/form-data" }))
    {
      @* a bunch of other stuff *@


      <div class="form-group">
        <h5>Images</h5>
        <div class="col-md-10">
            <table class="table table-bordered" id="dataTable" width="100%" cellspacing="0">
                <thead>
                    <tr>
                        <th>Image Prev</th>
                        <th>Name</th>
                    </tr>
                </thead>
                <tbody>
                    @foreach (var img in Model.TWebImages)
                    {
                        <tr>
                            <td><img src="~/ProductImages/@img.varImage" class="img-fluid" width="150" height="150" /></td>
                            <td>@img.varImage</td>
                        </tr>
                    }
                </tbody>
            </table>

            <h5>Upload Images</h5>

            <div>
                <div id="previews" class="dz-default dz-message box__input dropzone border">
                    <br/>
                    <div style="text-align:center">
                        <i class="fa fa-cloud-upload" style="font-size:23px;position:relative;top:4px;"></i> <span style="margin-left:20px">Drop files  to attach or browse</span>
                    </div>
                    <br />
                </div>
                <div id="previewFiles" class=""></div>
            </div>

        </div>
    </div>

     @* a bunch of other stuff *@

     <div class="form-group">
        <div class="col-md-offset-2 col-md-10">
            <input type="submit" value="Save" class="btn btn-primary" />
        </div>
    </div>
    }
    @section scripts{

<script>

    $(document).ready(function () {
        Dropzone.autoDiscover = false;
        $('#myDropzone').dropzone({
            //parameter name value
            paramName: "files",
            //clickable div id
            clickable: '#previews',
            //preview files container Id
            previewsContainer: "#previewFiles",
            autoProcessQueue: false,
            uploadMultiple: true,
            parallelUploads: 100,
            maxFiles: 100,
            //url:"../ProductImages/", // url here to save file
            maxFilesize: 100,//max file size in MB,
            addRemoveLinks: true,
            dictResponseError: 'Server not Configured',
            //acceptedFiles: ".png,.jpg,.gif,.bmp,.jpeg,.pdf",// use this to restrict file type
            acceptedFiles: ".png,.jpg,.gif,.bmp,.jpeg",// use this to restrict file type
            init: function () {
                var self = this;
                // config
                self.options.addRemoveLinks = true;
                self.options.dictRemoveFile = "Delete";
                //New file added
                self.on("addedfile", function (file) {
                    console.log('new file added ', file);
                    $('.dz-success-mark').hide();
                    $('.dz-error-mark').hide();
                });
                // Send file starts
                self.on("sending", function (file, xhr, formData) {
                    console.log('upload started', file);
                    $('.meter').show();
                });

                // File upload Progress
                self.on("totaluploadprogress", function (progress) {
                    console.log("progress ", progress);
                    $('.roller').width(progress + '%');
                });

                self.on("queuecomplete", function (progress) {
                    $('.meter').delay(999).slideUp(999);
                });

                // On removing file
                self.on("removedfile", function (file) {
                    console.log(file);
                });

                $('#Submit').on("click", function (e) {
                    e.preventDefault();
                    e.stopPropagation();
                    // Validate form here if needed

                    if (self.getQueuedFiles().length > 0) {
                        self.processQueue();


                    } else {
                        self.uploadFiles([]);
                        $('#myDropzone').submit();
                    }

                });



                self.on("successmultiple", function (files, response) {
                    // Gets triggered when the files have successfully been sent.
                    // Redirect user or notify of success.
                    $(".alert").alert('close');
                });
            }
        });


    })

</script>

} }

Also on the edit page html This is the ProductEdit.cshtml part where the drop and drag go同样在编辑页面 html这是 ProductEdit.cshtml 部分,其中拖放 go

This is the script for the dropzone on the product edit page Script on the ProductEdit.cshtml这是产品编辑页面上 dropzone 的脚本 ProductEdit.cshtml 上的脚本

These are the errors from the dev tools from chrome DevTools Error messages这些是来自 chrome DevTools 错误消息的开发工具的错误

I have little to no experience with javascript, very small experience (like 40 hours of experience) in MVC, but am decently well versed in C#我对 javascript 几乎没有经验,在 MVC 中的经验非常少(比如 40 小时的经验),但我非常精通 C#

Please help me find what I'm doing wrong.请帮我找出我做错了什么。 Let me know if I need to clarify anything better.如果我需要更好地澄清任何事情,请告诉我。

I found the answer, the submit button didn't have the name attribute.我找到了答案,提交按钮没有名称属性。 This was not calling the javascript to actually save the images.这并不是调用 javascript 来实际保存图像。

old submit button: <input type="submit" value="Save" class="btn btn-primary" />旧的提交按钮: <input type="submit" value="Save" class="btn btn-primary" />

New submit button: <input type="submit" value="Submit" id="Submit" name="Submit" class="btn btn-primary" />新提交按钮: <input type="submit" value="Submit" id="Submit" name="Submit" class="btn btn-primary" />

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM