我写了一个类并设置了公共IActionResult Index():
public class SuperBowl
{
public string Name { get; set; }
public double Price { get; set; }
public bool GiftPackage { get; set; }
}
[HttpGet]
public IActionResult Index()
{
double[] pricearraying = { 630, 500.25, 385.75 };
string[] namearraying = { "Red", "Green", "Blue" };
var heyinitial = new List<SuperBowl>();
for (int i = 0; i < pricearraying.Length; i++)
{
heyinitial.Add(new SuperBowl { Name = namearraying[i], Price = pricearraying[i], GiftPackage = false });
}
return View(heyinitial);
}
[HttpPost]
public IActionResult Index(List<SuperBowl> ceko)
{
double[] exposalary = { 63.00, 42.00, 21.00 };
var choosing = new List<double>();
foreach (var item in ceko.Select(x=>x.GiftPackage))
{
if (item == true)
{
choosing.Add(exposalary[ceko.FindIndex(r=>r.GiftPackage==item)]);
}
}
}
return View("MyCheckOut",choosing);
}
这是我的Index.cshtml:
@model List<SuperBowl>
@{
}
<!DOCTYPE html>
<html>
<head>
<title>Index</title>
</head>
<body>
<form asp-action="Index" asp-controller="Home" method="post">
<div class="table-responsive-sm table-hover">
<table align="center" class="table table-bordered">
<thead>
<tr class="table-light text-center">
<th>Item Name</th>
<th>My Price</th>
<th>Hey Gift</th>
</tr>
</thead>
<tbody>
@foreach (var st in Model)
{
<tr class="text-center">
<td asp-for="@st.Name">@st.Name</td>
<td asp-for="@st.Price">@st.Price</td>
<td asp-for="@st.GiftPackage">@Html.CheckBoxFor(m => st.GiftPackage)</td>
</tr>
}
</tbody>
</table>
<input type="submit" value="CheckOut" class="btn btn-primary" />
</div>
</form>
</body>
</html>
这是MyCheckOut.cshtml:
@model List<double>
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Hello</title>
</head>
<body>
<div class="table-responsive-sm table-hover">
<table align="center" class="table table-bordered">
<thead>
<tr class="table-light text-center">
<th>Our Discount Numbers</th>
</tr>
</thead>
<tbody>
@foreach (var discounting in Model)
{
<tr class="text-center">
<td>@discounting</td>
</tr>
}
</tbody>
</table>
</div>
</body>
</html>
我的期望是当我只勾选指示0为0的红色时,然后通过相同的指数0,让我们得到阵列公开[0],这是63.00,或者当我勾选红色和蓝色时这些指数是0和2,然后通过相同索引0和2,让我们得到数组公开[0]和公开[2],它们是63和21。
如果我部署所有这些代码,在Index.cshtml中我的表似乎很好,另一方面,当我选择任何复选框提交并在MyCheckOut中发送我的选择时,每个数据都是OK,没有元素。
请帮我。
你的代码中有几个问题,例如你的页面中的GiftPackage
不是GiftPacage
。
复杂对象绑定的关键是确保将方括号中的顺序索引添加到表单字段的name属性中。所以将你的Index.cshtml
修改为:
@for (int i = 0; i < Model.Count; i++)
{
<tr class="text-center">
<td asp-for="@Model[i].Name">@Model[i].Name</td>
<td asp-for="@Model[i].Price">@Model[i].Price</td>
<td asp-for="@Model[i].GiftPackage">@Html.CheckBoxFor(m => Model[i].GiftPackage)</td>
</tr>
}
另一个问题是findIndex()
方法返回数组中第一个满足提供的测试函数的元素的索引。因此,如果从复选框中进行多项选择,您将获得多个相同的值。将您的操作代码更新为:
[HttpPost]
public IActionResult Index(List<SuperBowl> ceko)
{
double[] exposalary = { 63.00, 42.00, 21.00 };
var choosing = new List<double>();
for (int i = 0; i < ceko.Count; i++)
{
if (ceko[i].GiftPackage ==true)
{
choosing.Add(exposalary[i]);
}
}
return View("MyCheckOut", choosing);
}