Crystal Rouse
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(cart Item);
}
}
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("GeneralI nfo");
}
The current View has the following:
@model master.Areas.BillofMateria l.Models.t bl_Type[,]
@{
ViewBag.Title = "Home";
master.Areas.BillofMateria l.Models.t bl_Cart cart = new master.Areas.BillofMateria l.Models.t bl_Cart();
Layout = "~/Areas/BillofMaterial/Vi ews/Shared /_Layout.c shtml";
}
@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.
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(cart
}
}
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("GeneralI
}
The current View has the following:
@model master.Areas.BillofMateria
@{
ViewBag.Title = "Home";
master.Areas.BillofMateria
Layout = "~/Areas/BillofMaterial/Vi
}
@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",
@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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.