i'm new to asp.net mvc.I have a list of checkboxes and i want when the checkboxes are selected a new list of selected checkboxs are shown. my code Product.cs code:
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; }
}
My view:
<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" />
}
This is my Controller code.I want when the check boxes are selected in a new page the selected check boxes are shown. my 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>