2

这是我的 JavaScript 代码:

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 代码中推送每个玩家信息。

4

1 回答 1

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();
    }
于 2012-07-01T03:44:06.430 回答