2012-06-30 94 views
2

這是我的javascript代碼的JavaScript推對象數組:獲取一個MVC3控制器動作

var bankOptions = {}; 
    var playerOptions = []; 
    bankOptions["BankTotalAmount"] = $("#totalBankAmountID").val(); 
    bankOptions["SinglePlayerAmount"] = $("#singlePlayerAmountID").val(); 
    while (_playerNumber != 0) { 

     if (_playerNumber == 1) { 
      var player1Option = {}; 
      player1Option["Name"] = $("#p" + _playerNumber + "Name").val(); 
      player1Option["Color"] = $("#p" + _playerNumber + "Color").val(); 

      playerOptions.push(player1Option); 
     } 

     if (_playerNumber == 2) { 
      var player2Option = {}; 
      player2Option["Name"] = $("#p" + _playerNumber + "Name").val(); 
      player2Option["Color"] = $("#p" + _playerNumber + "Color").val(); 

      playerOptions.push(player2Option); 
     } 

     if (_playerNumber == 3) { 
      var player3Option = {}; 
      player3Option["Name"] = $("#p" + _playerNumber + "Name").val(); 
      player3Option["Color"] = $("#p" + _playerNumber + "Color").val(); 

      playerOptions.push(player3Option); 
     } 

     if (_playerNumber == 4) { 
      var player4Option = {}; 
      player4Option["Name"] = $("#p" + _playerNumber + "Name").val(); 
      player4Option["Color"] = $("#p" + _playerNumber + "Color").val(); 

      playerOptions.push(player4Option); 
     } 

     _playerNumber--; 
    } 
    alert(playerOptions); 
    $.ajax({ 
     url: "/StartOption/setOptions/", 
     contentType: 'application/json', 
     data: JSON.stringify({ bankOptions: bankOptions, playerOptions: playerOptions }), 
     type: "POST", 
     timeout: 10000, 
     success: function (result) { 

     } 
    }); 

和我有這個控制器類

public class StartOptionController : Controller 
    { 
     private MonopolyDB db = new MonopolyDB(); 

     // 
     // GET: /StartOption/ 

     public ActionResult Index() 
     { 
      return View(); 
     } 

     [HttpPost] 
     public ActionResult setOptions(BankOptions bankOptions, Playeroptions[] playerOptions) 
     { 
      //int length = (int)playerOptions.GetType().InvokeMember("length", BindingFlags.GetProperty, null, playerOptions, null); 
      BankAccount bankaccount = new BankAccount(); 
      bankaccount.ID = 1; 
      bankaccount.TotalAmmount = bankOptions.BankTotalAmount; 
      db.BankAccount_Table.Add(bankaccount); 
      db.SaveChanges(); 

      //Here i want to get each (player1Option, player2Option...) object array from that playerOptions object array 


      //return RedirectToAction("Index"); 

      return View(); 
     } 

    } 
    public class BankOptions 
    { 
     public int BankTotalAmount { get; set; } 
     public int SinglePlayerAmount { get; set; } 
    } 
    public class Playeroptions 
    { 
     public string Name { get; set; } 
     public string Color { get; set; } 
    } 

我的問題是我怎麼可以讓這些對象數組我在我的setOptions操作中推入playerOptions對象數組?

就像我想從playerOptions對象數組中的每個玩家信息保存在我的數據庫中,我將每個玩家信息推送到我的JavaScript代碼中。

回答

3

那麼首先可以很容易,我想建議改變你的行動

public ActionResult setOptions(BankOptions bankOptions, Playeroptions[] playerOptions) 

public ActionResult setOptions(BankOptions bankOptions, List<PlayerOptions> playerOptions) 

這就是它要使它很容易的符號數組中每個元素的句柄,並且框架沒有序列化這個對象的問題。

現在回答你的問題,你可以重複這樣的

[HttpPost] 
    public ActionResult setOptions(BankOptions bankOptions, Playeroptions[] playerOptions) 
    { 
     //int length = (int)playerOptions.GetType().InvokeMember("length", BindingFlags.GetProperty, null, playerOptions, null); 
     BankAccount bankaccount = new BankAccount(); 
     bankaccount.ID = 1; 
     bankaccount.TotalAmmount = bankOptions.BankTotalAmount; 
     db.BankAccount_Table.Add(bankaccount); 
     db.SaveChanges(); 

     //Here i want to get each (player1Option, player2Option...) object array from that playerOptions object array 
     for (int i = 0 ; i< playerOptions.Length, i++) 
     { 
      playerOptions[i]; //<-- this give's the specific element 
     } 

     //return RedirectToAction("Index"); 

     return View(); 
    } 

陣列不過,如果你按照我的建議,並改變了你的行動的標誌,你可以重複這樣的

[HttpPost] 
    public ActionResult setOptions(BankOptions bankOptions, List<PlayerOptions> playerOptions) 
    { 
     //int length = (int)playerOptions.GetType().InvokeMember("length", BindingFlags.GetProperty, null, playerOptions, null); 
     BankAccount bankaccount = new BankAccount(); 
     bankaccount.ID = 1; 
     bankaccount.TotalAmmount = bankOptions.BankTotalAmount; 
     db.BankAccount_Table.Add(bankaccount); 
     db.SaveChanges(); 

     //Here i want to get each (player1Option, player2Option...) object array from that playerOptions object array 
     foreach(var item in playerOptions){ 
      item //<--- in this variable you have the element PlayerOption 
     } 

     //return RedirectToAction("Index"); 

     return View(); 
    } 
+0

我你的代碼剛剛在昨天處理了這個問題,我同意使用列表選項可能是迄今爲止最容易處理的選項。 – Chris

+0

Yeap,我在幾個月前處理這個問題......框架的偉大是它沒有做任何事情的序列化句柄 – Jorge

相關問題