gpt4 book ai didi

javascript - 在 MVC3 Controller 操作中获取 javascript 推送对象数组

转载 作者:行者123 更新时间:2023-11-29 20:08:03 25 4
gpt4 key购买 nike

这是我的 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) {

}
});

我有这个 Controller 类

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 代码中。

最佳答案

好吧,首先为了简单起见,我想建议您更改标志的操作

来自

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();
}

关于javascript - 在 MVC3 Controller 操作中获取 javascript 推送对象数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11274618/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com