您好我将数据发布到控制器有问题。现在我有一个模型,
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; }
}
然后我将文件和此模型发布到控制器。 在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;
}
});
我把这个帖子数据放在控制器中,如
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。我怎么解决这个问题? 谢谢你
答案 0 :(得分:5)
因为Folder是对象的集合,所以你必须为它们添加一个索引。
var index = 0;
for(var pair of folderss){
var folder = pair[key];
formData.append("Folders[" + index + "].Id", folder.Id);
index++;
}