I want to pass all data to controller after clicking button.
var data = new FormData();
Here is my javascript object
var info = {};
info.CorporateName = $("input[name*='CorporateName']").val();
info.CorporatePhone1 = $("input[name*='CorporatePhone1']").val();
info.CorporatePhone2 = $("input[name*='CorporatePhone2']").val();
info.CorporateEmail = $("input[name*='CorporateEmail']").val();
I can reach these datas from the controller with code below
$.each(info, function (key, input) {
data.append(key, input);
});
Here is my file list. I can reach these files from the controller with code below
var allfiles = $(".required-files");
for (var i = 0; i < allfiles.length; i++)
{
var files = allfiles[i].files;
data.append('files', files[0]);
}
Here is my packages cookie. I can't reach package list from the controller
var packageList = $.parseJSON($.cookie("packageList"));
data.append('packageList',JSON.stringify(packageList));
I can post files, my info object but I can't post cookie list
Here is my controller
[HttpPost]
public async Task<IActionResult> create(IEnumerable<IFormFile> files, List<PostServiceDto> packageList, ModelDto Model)
{
// files = not null, Model not null, but packageList = null
var model = Request.Form;
return View();
}
My package model
public class PostServiceDto
{
public string Type { get; set; }
public string Name { get; set; }
public string Price { get; set; }
public string Id { get; set; }
public string Count { get; set; }
}
I couln't take the list it with parameter but I solved it getting json object form request body.
the value returned from request ={\"Type\":\"3\",\"Name\":\"dfsfsdfs\",\"Price\":\"44,00\",\"Id\":\"2\",\"Count\":1},{\"Type\":\"3\",\"Name\":\"Yeni Paket\",\"Price\":\"49,90\",\"Id\":\"1\",\"Count\":1}
Solution
foreach (var item in Request.Form.Keys)
{
if(item == "packageList")
{
clearObject(Request.Form[item]);
}
}
public List<PostServiceDto> clearObject(string value)
{
List<PostServiceDto> postServices = new List<PostServiceDto>();
List<PostServiceDto> myDeserializedObjList =
(List<PostServiceDto>)Newtonsoft.Json.JsonConvert.DeserializeObject(value, typeof(List<PostServiceDto>));
return myDeserializedObjList;
}
code source : [https://www.codeproject.com/Tips/79435/Deserialize-JSON-with-C]