【发布时间】:2016-03-04 00:01:03
【问题描述】:
我是 asp.net mvc 的新手。我有一个复选框列表,我希望在选中复选框时显示一个新的选定复选框列表。 我的代码 Product.cs 代码:
public class Product
{
public int ProductID { get; set; }
public string ProductName { get; set; }
public int Price { get; set; }
public bool Checked { get; set; }
public virtual ICollection<Purchase> Purchases { get; set; }
}
我的看法:
<h2>Product Lists</h2>
@using (Html.BeginForm())
{
<table class="table">
<tr>
<th>
Product ID
</th>
<th>
Product Name
</th>
<th>
Price
</th>
<th></th>
</tr>
@for (var i = 0; i < Model.Count(); i++)
{
<tr>
<td>
@Html.DisplayFor(x => x[i].ProductID)
</td>
<td>
@Html.DisplayFor(x => x[i].ProductName)
</td>
<td>
@Html.DisplayFor(x => x[i].Price)
</td>
<td>
@Html.CheckBoxFor(x => x[i].Checked, new { Style = "vertical-align:3px}" })
</td>
</tr>
}
</table>
<input type="submit" value="Purchase" class="btn btn-default" />
}
这是我的控制器代码。我希望在新页面中选中复选框时显示选中的复选框。 我的 ActionResult:
public ActionResult Index()
{
return View(db.Products.ToList());
}
[HttpPost]
public ActionResult Index(List<Product> list)
{
return View(list);
}
@using (Html.BeginForm())
{
<table class="table">
<tr>
<th>
Product ID
</th>
<th>
Product Name
</th>
<th>
Price
</th>
<th></th>
</tr>
@for (var i = 0; i < Model.Count(); i++)
{
<tr>
<td>
@Html.DisplayFor(x => x[i].ProductID)
</td>
<td>
@Html.DisplayFor(x => x[i].ProductName)
</td>
<td>
@Html.DisplayFor(x => x[i].Price)
</td>
<td>
@Html.CheckBoxFor(x => x[i].Checked, new { Style = "vertical-align:3px}" })
</td>
</tr>
}
</table>
【问题讨论】:
-
以this answer为例
-
请多多帮助我..我无法解决我的问题
-
什么问题?什么不工作?你遇到了什么错误?
-
您是否遇到任何错误?如果你调试你的索引
POST,你的列表中有Checked == true吗? -
@ThiagoFerreira 是的,我在列表中检查了==true。但我想在新视图中显示这些真实检查。但我不知道该怎么做???!!!
标签: asp.net-mvc entity-framework-4