Currently, I am facing an issue with my REST API when trying to save arrays of "link" objects. The API works fine for adding a single "link" object as it binds to the model seamlessly. However, when passing an array of "link" objects, the List of strings does not bind correctly. I have attempted to use both a model that holds a List of Link and Link[] but encountered the same problem.
Here is the model structure:
public class Link
{
[JsonProperty(PropertyName = "name")]
public string name { get; set; }
[JsonProperty(PropertyName = "fields")]
public List<string> fields { get; set; }
[JsonProperty(PropertyName = "imgPath")]
public string imgPath { get; set; }
[JsonProperty(PropertyName = "category")]
public string category { get; set; }
}
The function in the controller:
public string PostLinkList([FromBody] List<Link> links)
{
// Function logic here...
return JsonConvert.SerializeObject(links);
}
The frontend implementation:
$(".category").each(function ()
{
// Frontend script here...
});
// AJAX call snippet here...
This is what the data format looks like as a string, which is passed to the backend:
"[
{\"name\":\"test name\",
\"fields\":[\"field\",\"details\"],
\"imgPath\":\"optImg icon-print\",
\"category\":\"test\"},
{\"name\":\"test name\",
\"fields\":[\"field\",\"details\"],
\"imgPath\":\"optImg icon-print\",
\"category\":\"test\"},
{\"name\":\"test name\",
\"fields\":[\"field\",\"details\"],
\"imgPath\":\"optImg icon-print\",
\"category\":\"test\"}
]"