[英]Nested JSON values not binding MVC
在客戶端JavaScript中有一個游戲對象,在發送到服務器之前看起來像這樣:
在這里是服務器端,稍后請注意,所有屬性都已填寫,並且“問題”列表中填充了正確數量的問題,但是每個問題的屬性為null,而在客戶端,它們具有正確的值。
這是模型的代碼:
public class Game
{
public Game()
{
Questions = new List<Question>(5);
}
public int GameID { get; set; }
public Guid UserID { get; set; }
public Guid CurrentGameID { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public IEnumerable<Question> Questions { get; set; }
}
public class Question
{
public int ID { get; set; }
public string Text { get; set; }
public IEnumerable<int> Answers { get; set; }
public int SelectedAnswer { get; set; }
}
這是我將對象發送回服務器的方式:
// Send completed game back to server
$.post("Games/CompleteGame", currentGame, function (results)
{
// display results to user
}
基於Ek0nomik關於內容類型的評論,我重寫了ajax調用,將contentType設置為json:
$.ajax(
{
url: "Games/CompleteGame",
type: "POST",
data: JSON.stringify(currentGame),
contentType: "application/json",
success: function (results)
{
// show results to user...
}
事實證明,這就是使其工作所需的全部。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.