ASP.Net 带 DropZone 的 MVC 5 无法向控制器发送附加参数

ASP.Net MVC 5 with DropZone Can't Send Additional Parameters to Controller

好的,我相信这已经在某个地方找到了答案,但我可以诚实地告诉你,我尝试过的一切都失败了。这是我当前的配置:

已共享 _Layout.cshtml(在页脚下方)

<script src="~/lib/jquery/dist/jquery.min.js"></script>
    <script src="~/lib/bootstrap/dist/js/bootstrap.bundle.min.js"></script>
    <script src="~/js/site.js" asp-append-version="true"></script>
    <script src="https://unpkg.com/dropzone@5/dist/min/dropzone.min.js"></script>
    @await RenderSectionAsync("Scripts", required: false)

图片查看

@model NewProductVM
<form id="form2" method="post" enctype="multipart/form-data" data-parsley-validate class="form-horizontal form-label-left">    
    <div class="form-group">
        <label class="control-label col-md-2" for="first-name">
             <span class="required">*</span>
        </label>
        
        <input type="hidden" value="@Model.Id" />

        <div class="col-md-6" id="dropzone">
            <div action="UploadFiles" class="dropzone" id="uploader">
                Drop files here or click to upload.<br>
            </div>
        </div>
    </div>
</form>

产品控制器

[HttpPost]
public async Task<IActionResult> UploadFiles(string Id)
{
            bool bSuccess = true;            

            _ConnectionString = _Configuration.GetConnectionString("StorageConnectionString");
            _ContainerName = _Configuration.GetValue<string>("StorageContainerName");

            string uploads = Path.Combine(_hostingEnvironment.ContentRootPath, "Uploads");

            if (!Directory.Exists(uploads))
                Directory.CreateDirectory(uploads);

            foreach (var iFormFile in Request.Form.Files)
            {
                if (iFormFile.Length > 0)
                {
                    if (StorageHelper.IsImage(iFormFile))
                    {
                        var filePath = Path.Combine(uploads, iFormFile.FileName);
                        using (var stream = new FileStream(filePath, FileMode.Create))
                        {
                            await iFormFile.CopyToAsync(stream);
                        }

                        bSuccess = await StorageHelper.UploadFileToStorage(iFormFile, filePath, _ConnectionString, _ContainerName);
                        System.IO.File.Delete(filePath);

                        ProductImage productImage = new ProductImage
                        {
                            ProductId = int.Parse(Id),
                            ImageURL = $"{_Configuration.GetValue<string>("StorageContainerURL")}/{iFormFile.FileName}"                            
                        };

                        await _imageService.AddNewImageAsync(productImage);                        
                    }
                }
            }
            return RedirectToAction("Index");
}

我真的很想将整个 NewProductVM 模型传递给 UploadFiles 方法,但是视图模型中的属性不是很多,所以我可以单独传递每个 属性。

现在我正在听取某人的建议创建一个隐藏的表单字段并添加一个我想作为表单字段传递给控制器​​的值,但是 UploadFiles 方法中的参数值为空。我也尝试过使用 asp-route-id="@Model.Id"。当我这样做时,控制器方法中 id 的值为 0。

有人有什么建议吗?

好的,我终于找到了答案,我不敢相信它是如此简单。我已经为此工作了几个小时。我在这里找到了答案:https://social.msdn.microsoft.com/Forums/en-US/a87c6936-c0b5-4f47-b074-dbaf4c154cdd/id-parameter-returning-null-after-using-formdataappend-to-append-id-to-model-id-in-mvc-5?forum=aspmvc

这是我所做的:

Images.cshtml

<form id="form2" method="post" enctype="multipart/form-data" data-parsley-validate class="form-horizontal form-label-left">
    <div class="form-group">
            
        <label class="control-label col-md-2" for="first-name">
            <span class="required">*</span>
        </label>

        <div class="col-md-6" id="dropzone">
            <div action="UploadFiles?Id=@Model.Id" class="dropzone" id="uploader">
                Drop files here or click to upload.<br>
            </div>

        </div>
    </div>
</form>   

ProductsController.cs

[HttpPost]        
public async Task<IActionResult> UploadFiles(int Id)
{
    bool bSuccess = true;            

    _ConnectionString = _Configuration.GetConnectionString("StorageConnectionString");
    _ContainerName = _Configuration.GetValue<string>("StorageContainerName");

    //int Id = int.Parse(fc["Id"]);

    string uploads = Path.Combine(_hostingEnvironment.ContentRootPath, "Uploads");

    if (!Directory.Exists(uploads))
        Directory.CreateDirectory(uploads);

    foreach (var iFormFile in Request.Form.Files)
    {
        if (iFormFile.Length > 0)
        {
            if (StorageHelper.IsImage(iFormFile))
            {
                var filePath = Path.Combine(uploads, iFormFile.FileName);
                using (var stream = new FileStream(filePath, FileMode.Create))
                {
                    await iFormFile.CopyToAsync(stream);
                }

                bSuccess = await StorageHelper.UploadFileToStorage(iFormFile, filePath, _ConnectionString, _ContainerName);
                System.IO.File.Delete(filePath);

                //ProductImage productImage = new ProductImage
                //{
                //    ProductId = Id,
                //    ImageURL = $"{_Configuration.GetValue<string>("StorageContainerURL")}/{iFormFile.FileName}"          
                //};

                //await _imageService.AddNewImageAsync(productImage);                        
            }
        }
    }
    return RedirectToAction("Index");
}

真正让它发挥作用的部分是 action="UploadFiles?Id=@Model.Id"

所以基本上,我使用操作将我想要的值传递给我的控制器方法。