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

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐