简体   繁体   中英

CheckBoxList in Asp.Net MVC4

I am trying to implement a CheckBoxList in my View and get the selected list of checkbox values in a List.

I tried the example from this link , but I can't get the selected checked list items.

My View

@foreach (var item in Model)
{
    <tr>
        <td align="center" class="Text_nocolor singleCheckbox">
            <input type="checkbox" class="checkbox" value="@item.ProductID" name="selectedObjects"/>
            @Html.HiddenFor(modelItem => item.ProductName)
        </td>
        <td align="center" class="Text_nocolor" id="myimage">
            <img src="@Url.Content(item.Imageurl)" width="40px" height="40px"  title="@item.ProductName" alt="Product"  class="myimage" style="border-style:none;"/>
        </td>
        <td align="left" class="Text_nocolor">
            @Html.DisplayFor(modelItem => item.ProductName)
        </td>
    </tr>
}
@Html.ActionLink(" ", "Checkout", new { controller = "Checkout", UserID = Request.QueryString["UserID"], Partnerid = Request.QueryString["Partnerid"] }, new { @class = "btnCheckout" })

Controller code (Index) :

SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["DBConnection"].ConnectionString);
public IList<CartModel> GetCartDetails(string id, string partnerid)
{
    List<CartModel> CartDetails = new List<CartModel>();
    {
        con.Open();
        SqlCommand cmd = new SqlCommand("select * from tablename where [User ID]='" + id + "' and [Partner ID]='" + partnerid + "'", con);
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataTable dt = new DataTable();
        da.Fill(dt);
        if (dt.Rows.Count > 0)
        {
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                CartModel cartInfo = new CartModel();
                cartInfo.ProductName = dt.Rows[i]["Product Name"].ToString();
                cartInfo.Imageurl = "~/Handler.ashx?Pid=" + dt.Rows[i]["Product ID"].ToString() + "&Imgid=1&Partnerid=" + partnerid;
                CartDetails.Add(cartInfo);
            }
        }
        con.Close();
    }
    return CartDetails;
}

public ActionResult Index()
{
    id = Request.QueryString["UserID"];
    partnerid = Request.QueryString["Partnerid"];
    IList<CartModel> objshop = new List<CartModel>();
    objshop = GetCartDetails(id, partnerid);
    ViewBag.Categories = objshop.ToList();
    return View(objshop.ToList());
}

public ActionResult Checkout(string id, string partnerid, int[] categories)
{
    string id = Request.QueryString["UserID"];
    string partnerid = Request.QueryString["Partnerid"];
    //How to get the selected checked list items as a list.
    return View();
}

Any suggestions?

Finally I got this through a Webgrid to get a checkboxlist.

Refer this Blog http://weblogs.asp.net/imranbaloch/archive/2011/09/13/webgrid-helper-with-check-all-checkboxes.aspx

  @grid.GetHtmlWithSelectAllCheckBox(tableStyle: "webGrid",
            headerStyle: "header",
            checkBoxValue: "ProductID",
            columns: grid.Columns(
            grid.Column(columnName: "", format: @<text><img src="@Url.Content(item.Imageurl)" class="image" alt="Image "/></text>, style: "txtalignleft"),
            grid.Column(columnName: "ProductName", style: "txtalignleft"),
            grid.Column(columnName: "Quantity", style: "txtcenter"),
            grid.Column("Edit", " ", style: "txtalignleft", format: @<a href="@Url.Action("Index", "Cart", new { UserID = Request.QueryString["UserID"], partnerid = Request.QueryString["Partnerid"] })"><img
                alt="edititem" style="vertical-align: middle; text-align: justify; border-style: none;"
                height="17px" src="../../Images/edit.png" title="Edit" id="imgEdit" /></a>),
            grid.Column(columnName: "Rate", style: "txtalign"),
            grid.Column(columnName: "Price", style: "txtalign")
      ))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM