我编写了一个类并设置了公共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时,我们得到数组exposalary [0]为63.00 或当我勾选这些索引分别为0和2的红色和蓝色时,然后通过相同的索引0和2,我们得到数组exposalary [0]和exposalary [2]分别为63和21。
如果我部署了所有这些代码,则在Index.cshtml中,我的表看起来不错,并且当我选择任何复选框提交并在MyCheckOut中发送我的选择时,每个数据都可以,
请帮助我。
答案 0 :(得分:0)
您的代码中有几个问题,例如页面中的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);
}