2013-04-12 51 views
1

我在客戶端的JavaScript遊戲的對象,看起來像這樣的權利被髮送到服務器之前:嵌套JSON值不具約束力MVC

Client Side

這是一秒鐘後服務器端,注意所有屬性被填充,並且問題列表被填充正確數量的問題,但是每個問題的屬性都是空的,而在客戶端它們具有正確的值。

Server Side

下面是機型代碼:

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 
} 
+3

你可以顯示POST中發送的原始JSON嗎?另外,你可以驗證內容類型嗎? –

+0

@ Ek0nomik我不知道如何在這種情況下獲得原始JSON。 –

回答

1

基於Ek0nomik的評論詢問內容類型,我重寫了我的ajax調用將contentType設置爲json:

$.ajax(
    { 
     url: "Games/CompleteGame", 
     type: "POST", 
     data: JSON.stringify(currentGame), 
     contentType: "application/json", 
     success: function (results) 
     { 
      // show results to user... 
     } 

事實證明,這是使其工作所需的一切。