您好,我在将数据发布到控制器时遇到问题。现在我下面有一个模型,
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 ListPortals { get; set; } public string Lang { get; set; } public List Folders { get; set; } }
然后,我将文件和该模型发布到控制器。In AJAX侧面
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; } });
我把这个帖子数据放在控制器中
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) })); }
我没有文件夹属性的所有数据都为空。我怎么解决这个问题?感谢你
因为Folder是对象的集合,所以您必须为每个对象添加索引。
var index = 0; for(var pair of folderss){ var folder = pair[key]; formData.append("Folders[" + index + "].Id", folder.Id); index++; }