Link to home
Start Free TrialLog in
Avatar of Crystal Rouse
Crystal RouseFlag for United States of America

asked on

Submit a Form in a View with an array.

I have a View that I need to update to a form submit for security reasons.  I need to be able to use HttpPost instead of HttpGet for my controller action.  I need help converting this.  

The controller is:

 [HttpPost]
        public ActionResult SelectType(int type)
        {
            if (user.bomDetails.type != type)
            {
                List<tbl_Cart> cartItems = (from x in bomDB.tbl_Cart
                                            where x.UserID == user.id
                                            select x).ToList();

                foreach (tbl_Cart cartItem in cartItems)
                {
                    bomDB.tbl_Cart.Remove(cartItem);
                }
            }

            tbl_User_Bom bomUserMe = (from x in bomDB.tbl_User_Bom
                                      where x.ID == user.id
                                      select x).Single();
            bomUserMe.Type = type;
            bomDB.SaveChanges();

            return RedirectToAction("GeneralInfo");
        }

The current View has the following:

@model master.Areas.BillofMaterial.Models.tbl_Type[,]
@{
    ViewBag.Title = "Home";
    master.Areas.BillofMaterial.Models.tbl_Cart cart = new master.Areas.BillofMaterial.Models.tbl_Cart();
    Layout = "~/Areas/BillofMaterial/Views/Shared/_Layout.cshtml";
}

@for (int i = 0; i < Model.GetLength(0); i++)
{
    <div class="row">
        @for (int j = 0; j < Model.GetLength(1); j++)
        {
            string offset = "col-xs-offset-2";
            bool isCurrent, isNull;
            if (j == 0)
            {
                offset = "col-xs-offset-1";
            }
           
            try
            {
                isNull = Model[i, j].Description.Length < 1;
            }
            catch
            {
                isNull = true;
            }
           
            if (!isNull)
            {
                isCurrent = ViewBag.myType != Model[i, j].ID && cart.CountCart > 0;
                string x = isCurrent ? "true" : "false";
               
                <div class="col-xs-2 @offset" style="height: 75px;">
                    <button type="button" class="btn btn-success" style="height: 100%; width: 100%; white-space: normal;" onclick="if(@x && confirm('You are about to empty your cart')){window.location = '@Url.Action("SelectType", new { @type = Model[i, j].ID })'} else if(!@x){window.location = '@Url.Action("SelectType", new { @type = Model[i, j].ID })'}">
                        @Model[i, j].Description
                    </button>
                </div>
            }
        }
    </div>
}

I know I have to use a  @using (Html.BeginForm with a Hidden Field for the Type. I just don't know how to do this with an array.
ASKER CERTIFIED SOLUTION
Avatar of Crystal Rouse
Crystal Rouse
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial