그래서 "링크"개체를 저장하는 나머지 API가 있습니다. 이제 나머지 API는 "link"객체를 한 번에 하나씩 추가하여 모델에 잘 바인딩하지만, "link"객체의 배열이 전달 될 때 문자열 목록을 바인딩하지 않습니다. 나는 List of Link와 Link []를 가지고있는 모델을 사용하려고 시도했다.모델 내부의 배열이 모델이 배열의 일부일 때 바인딩되지 않습니다.
모델 컨트롤러이
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; }
}
함수 문자열 포맷 보이는 바와 같이, 선단이
$(".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"); }
});
HES 어떤 모양
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);
}
이다 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