-1

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>
B-CHI
  • 139
  • 3
  • 14
  • Refer [this answer](http://stackoverflow.com/questions/29542107/pass-list-of-checkboxes-into-view-and-pull-out-ienumerable/29554416#29554416) for an example –  Mar 03 '16 at 10:53
  • please help me more..I can't solve my problem – B-CHI Mar 03 '16 at 11:42
  • What problem? What is not working? What errors are you getting? –  Mar 03 '16 at 11:45
  • Are you getting any errors? If you debug your Index `POST`, is there any `Checked == true` in your list? – Thiago Ferreira Mar 03 '16 at 11:47
  • @ThiagoFerreira yes I have checked==true in my list.but i want to show these true checks in a new view.but i don't know how to do this???!!! – B-CHI Mar 03 '16 at 14:38
  • @StephenMuecke I want to get a list of true checkboxes and show them in a new page – B-CHI Mar 03 '16 at 14:40
  • @ThiagoFerreira thank u..my problem is solved.but when redirect to a new view my product id,price is zero and my product name is empty.this is my new view – B-CHI Mar 03 '16 at 15:58
  • This happens because `DisplayFor` doens't POST your values. Do something like for each field that is empty or zero and the problem will be solved: ` @Html.DisplayFor(x => x[i].ProductID) @Html.HiddenFor(x => x[i].ProductID) ` – Thiago Ferreira Mar 03 '16 at 16:45

1 Answers1

3

If you already have a List with all checked/unchecked properties and just want to show the checked records in a new view, you can store your list in a TempData and redirect to an action which will use your list:

public ActionResult Index()
{
    return View(db.Products.ToList());
}

[HttpPost]
public ActionResult Index(List<Product> list)
{
    TempData["CheckedRecords"] = list.Where(x=>x.Checked).ToList(); //Don't forget to add 'using System.Linq;'!
    return RedirectToAction("MyOtherView");
}

public ActionResult MyOtherView()
{
    var checkedRecords = (List<Product>)TempData["CheckedRecords"];
    return View(checkedRecords);
}
Thiago Ferreira
  • 661
  • 3
  • 19
  • And if the user refreshes the browser, this all fails. Save the data to the database! –  Mar 03 '16 at 23:10