ajax form.append属性,jquery - Ajax FormData append list object - Stack Overflow
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 s
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 Portals { get; set; }
public string Lang { get; set; }
public List Folders { get; set; }
}
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) }));
}
I take all data without Folders attribute it comes null. How can I solve this problem?
Thank u
更多推荐
所有评论(0)