所以我有一个保存“链接”对象的其余API。现在剩下的API用于添加“链接”对象,一次绑定到模型上,但是当它传递一个“链接”对象数组时,它不绑定字符串列表。我已经尝试使用包含链接列表和链接[]的模型。当模型是数组的一部分时,模型内部的数组没有绑定
模型是这样的
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; }
}
控制器的功能是
public string PostLinkList([FromBody] List<Link> links)
{
string json;
bool waiting = true;
int timeWaiting = 0;
do
{
try
{
using (StreamWriter outfile = new StreamWriter("App_Data/linkObjs.txt", false))
{
for (int i = 0; i < links.Length; i++)
{
json = JsonConvert.SerializeObject(links[i]);
outfile.Write(json + "\n");
}
}
waiting = false;
}
catch (Exception e)
{
timeWaiting++;
if (timeWaiting > 20)
{
waiting = false;
}
}
} while (waiting);
return JsonConvert.SerializeObject(links);
}
前端看起来像这样
$(".category").each(function()
{
var tag = $(this).attr("id");
var category = $(this).find(".issueTitle").children("span").text();
$("#" + tag + " button").each(function()
{
var imgPath = $(this).children("i").attr("class");
var fields = $(this).attr("name").split(",");
var name = $(this).children("p").children("span").text();
links[i] = {
name: name,
fields: fields,
imgPath: imgPath,
category: category
};
i++;
});
});
$.ajax(
{
url: "/api/Link",
type: "Post",
async: false,
contentType:"application/json",
dataType: "json",
data: links,
success: function() { populate(); },
error: function() { alert("failed to populate"); }
});
祂什么,作为一个字符串,格式如下like /这是传递给后端的字符串
"[
{\"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\"}
]"
你可以发布你的json格式吗? – riteshmeher
肯定给我一分钟 – ZergRush
你有没有尝试过用数据:JSON.stringify(链接) – riteshmeher