I am trying to pass object from ajax to asp.net core
Here is asp code
[HttpPost]
public async Task<IActionResult> AddOrUpdate([FromForm] LSPost lsPost, List<IFormFile> files)
{
return await Task.Run<IActionResult>(() =>
{
try
{
JsonResult t = ImageUpload(files).Result as JsonResult;
string[] fullFilePathsReturned = t.Value as string[];
lsPost.Media = fullFilePathsReturned[0];
lsPost.Update();
return Ok();
}
catch (Exception ex)
{
return StatusCode(500);
}
});
}
and here is ajax call
function UploadFile() {
var lsPost = @Html.Raw(Newtonsoft.Json.JsonConvert.SerializeObject(new LSPost()));
lsPost.Title = $("#someText").val();
console.log(lsPost);
var fileData = new FormData();
fileData.append("lsPost", lsPost);
fileData.append("files", $("#someRandomID").get(0).files[0]);
$.ajax({
type: "POST",
url: "/LSPostModel/AddOrUpdate",
processData: false,
contentType: false,
data: fileData,
success: function () {
}
});
}
parameter "files" is passed but "lsPost" is not. I tried adding / removing [FromForm] / [FromBody]
tags. I tried matching first parameter of fileData.append
with parameter name on back. I tried matching javascript object name with parameter name on back. Nothing is working....