FormData追加列表对象

FormData追加列表对象

本文介绍了Ajax FormData追加列表对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我在将数据发布到控制器时遇到问题.现在我下面有一个模型,

Hello I have a problem to post data to controller. Now I have one model below,

public class Media
    {
        public int Id { get; set; }
        public string Category { get; set; }
        public string Guid { get; set; }
        public string Title { get; set; }
        public string Description { get; set; }
        public List<int> Portals { get; set; }
        public string Lang { get; set; }
        public List<Folder> Folders { get; set; }

       }

然后我将文件和该模型发布到控制器.In View AJAX侧面

Then I will post file and this model to controller.In View AJAX Side

var formData = new FormData();
            var totalFiles = document.getElementById("FileUpload").files.length;
            if (totalFiles === 0) {
                toastr.warning('Lütfen resim yükleyin.');
                return;
            }
            for (var i = 0; i < totalFiles; i++) {
                var file = document.getElementById("FileUpload").files[i];

                formData.append("FileUpload", file);
            }

            var itemFolder= {
                Id: refFolder
            }

            var folderss = [];
            folderss.push(itemFolder);

            var item = {
                Title: title,
                Description: desc,
                Category: category,
                Portals: portals,
                Folders: folderss
            }
            formData.append("Title", title);
            formData.append("Description", desc);
            formData.append("Category", category);
            formData.append("Portals", portals);
            formData.append("Folders",folderss);


            $.ajax({
                type: 'POST',
                url: '@Url.Action("Add", "Media")',
                data: formData,

                contentType: false,
                processData: false,
                success: function (data) {
                    var result = JSON.parse(data);
                    if (result.Status !== 200) {

                        toastr.error('@Resources.Resource.Error_Unexpected');
                        return;
                    }

                    if (result.Result === "SUCCEED") {
                        toastr.success('Resim kaydedilmiştir.');
                        window.location.reload();
                        return;
                    } else {
                        toastr.error('@Resources.Resource.Error_Unexpected');
                    }

                },
                error: function (error) {
                    toastr.error('@Resources.Resource.Error_Unexpected');
                    return;
                }
            });

我把这个帖子数据放在控制器中

And i take this post data in controller like

public ActionResult Add(Models.Media item)
        {
            if (item == null
                || string.IsNullOrEmpty(item.Title)
                || string.IsNullOrEmpty(item.Category))
                return Content(Serialization.JsonSerialize(new { Status = 400 }));

            if (Request.Files.Count <= 0)
                return Content(Serialization.JsonSerialize(new { Status = 401, Result = "NO_FILE" }));

            return Content(Serialization.JsonSerialize(new { Status = 200, Result = MediaRepository.Add(item) }));
        }

我获取了没有Folders属性的所有数据,该数据为null.我怎么解决这个问题?谢谢你

I take all data without Folders attribute it comes null. How can I solve this problem?Thank u

推荐答案

由于文件夹是对象的集合,因此必须为每个对象添加一个索引.

Because Folder is a collection of objects, you have to add each one of them with an index.

var index = 0;
for(var pair of folderss){
    var folder = pair[key];
    formData.append("Folders[" + index + "].Id", folder.Id);
    index++;
}

这篇关于Ajax FormData追加列表对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-30 23:46