I try to send an multi-language object to ProductsController, controller accepts the method with no error and object has the correct Id and Tag but the Names and Briefs arrays are empty!
I tried HttpPOST, HttpPUT, HttpOptions but got no success.
JS Code:
var dataToSend = new FormData();
var Names = $.makeArray();
var Briefs = $.makeArray();
Names.push({ LangCode: 'en', Value: 'TEST' });
Names.push({ LangCode: 'de', Value: 'TOST' });
Briefs.push({ LangCode: 'en', Value: 'FOO' });
Briefs.push({ LangCode: 'de', Value: 'BAR' });
dataToSend.append("Id", productId);
dataToSend.append("Tag", "SampleTag");
dataToSend.append("Names", Names);
dataToSend.append("Briefs", Briefs);
$.ajax({
type: 'PUT',
contentType: 'application/json; charset=utf-8',
url: "/api/products/" + productId,
data: dataToSend,
processData: false,
contentType: false,
success: function (data) {
DoST();
},
error: function (xhr, textStatus, error) {
var response = JSON.parse(xhr.responseText);
DoST();
}
});
The C# Code is like below: I used LangValue object to pass different language values through the application and It works as expected but not fills via ajax!
public class LangValue
{
public string LangCode { get; set; }
public string Value { get; set; }
}
public class Product
{
public int Id { get; set; }
public string Tag { get; set; }
public IList<LangValue> Names { get; set; }
public IList<LangValue> Briefs { get; set; }
}
[HttpPut("{id}")]
public async Task<IActionResult> PutProductLangs(int id, Product product)
{
PRODUCT Id and Tag are filled but Names and Briefs arrays are EMPTY!!!
return NoContent();
}