バインディング:モデルは、私はこのようなAJAX要求を作っている私のプロジェクトで、.NETのコアでのAjaxリクエスト
$(document).ready(function() {
var friends = [];
friends[0] = { Id: 1, Name: "John" };
friends[1] = { Id: 2, Name: "Mike" };
var obj = {
Id: 1,
Name: "Piotr",
Friends: friends
}
$.ajax({
contentType: 'application/json; charset=utf-8',
type: 'GET',
url: '/Home/Test',
data: obj
});
});
私は下のクラスにデータをバインドしようとしている:
public class Person
{
public long Id { get; set; }
public string Name { get; set; }
public IEnumerable<Friend> Friends { get; set; }
}
public class Friend
{
public long Id { get; set; }
public string Name { get; set; }
}
このようなアクションの結果では:
[HttpGet]
public IActionResult Test(Person p)
{
HttpRequest r = HttpContext.Request;
return Json(new
{
sEcho = "",
iTotalRecords = 2,
iTotalDisplayRecords = 2,
aaData = ""
});
}
問題は要求がなく、空白のFreiendsリストを使用したテストのアクションの結果をトリガしていることです。データはPersonクラスのFriendsプロパティにバインドされていません。
私が間違って何をしているのですか?
リクエストクエリ文字列は、このようなものです:あなたはにいくつかのことをする必要がある {?Id=1&Name=Piotr&Friends%5B0%5D%5BId%5D=1&Friends%5B0%5D%5BName%5D=John&Friends%5B1%5D%5BId%5D=2&Friends%5B1%5D%5BName%5D=Mike}
なぜHttpGetで行うのですか? HttpPostを試してください。 – levent
すべてのPerson pプロパティをポストするためにそれを変更しているときはnullまたは0です。 –