Wednesday, July 6, 2011

Handling Checkboxes at Controllers in MVC Part II

Hi

In continuation to my previous post here I am extending how on the basis of the selected checkboxes  we can remove the records form the session variable.

A small piece of code from the view to show how the names of the checkboxes are being given

foreach (Controllers.ProductsToBuy p in Model)
            {
                string chkname = "chk" + i.ToString();
                i++;
            <tr>
                <td align="center">
                    <input type="checkbox" value="" class="checkbox" runat="server" id="@chkname" />
                </td>

Code Discussion:
Using the foreach loop inside a table the checkboxes are also being rendered along with the product. And in the above example I have am setting the id property of the check box to chk and concatenating the rowno with this chk I would have concatenate the product id as well but it might possible that the user can buy the same product multiple times as I am not considering it for my validation for this scope so to avoid the confusion I am using the row number the logic can differ according to your need.

Following is the code snippet from the controller which is handling the post method of the view (form)

        [HttpPost]
        [ValidateInput(false)]
        public ActionResult ProductAddtoCart(FormCollection collection)
        {
            int i = 0;
            List<ProductsToBuy> p = new List<ProductsToBuy>();
            List<ProductsToBuy> newp = new List<ProductsToBuy>();
            p = ((List<ProductsToBuy>)Session["ProductCart"]);
            foreach (ProductsToBuy pb in p)
            {
                string chkname = "chk" + i.ToString();
                if (collection[chkname] == null)
                {
                    newp.Add(pb);
                }
                i++;
            }
            Session["ProductCart"] = newp;
            return View(newp);
        }
    }

Code Discussion:

The idea which I am following behind this is I will cast the list from the session variable which is of type list to the desired list<class>; I have also created a newp object of same type in which I will store the records which are need to be purchased by the customer you can also do the other way round u can remove the items which user don't want (or want to delete) and then using foreach loop I am checking whether the check box is checked or not as I know that I have given the id's to the checkboxes like chk0, chk1… so on as per row number.  So here I am initializing my string variable chkname so that I can create a string for the checkbox names by concatenating chk and the row number then I am checking the  chkname in the collection (which is passed by the view as a formcollection at time of postback) if the collection is null that means the checkbox is still unchecked and I am adding that item in my newp object and finally setting the session variable again to the newp object which now only contains the probuct which are not cecked at the front end and returning the same object to the view to display.



Thanks and Regards
Meetu Choudhary


0 comments:

Post a Comment

Subscribe via email

Enter your email address:

Delivered by FeedBurner

MSDotnetMentor

MSDotnetMentor My Website http://msdotnetmentor.com

Blog Archive