Working with a checkboxes, and stucked in transfer selected checkboxes to backend. I've an idea how to do this, but I want to hear another variants. So I've a table with checkboxes:
<td>
<input type="checkbox" class="check" value="@item.Id"/>
</td>
And submit button
@Html.ActionLink("delete", "DeleteSelectedPictures")
So my variant is to add bool property and change table from input
to @html.checkboxfor(_ => _.selected)
, and how then get these selected items? Is there others way how to solve this problem?
//controller logic
public ActionResult DeleteSelectedPictures(int itemsId)
{
var pictures = from items in _Db.Pictures
where items.Id == itemsId
select items;
foreach (var picture in pictures)
{
_Db.Pictures.Remove(picture);
}
return RedirectToAction("Index");
}
upd 2.
Due to Ali sultani suggestion, here is update, but have problem, have null on from here var selected = Request.Form["chkPicture"];
cshtml:
<div>
@using (Html.BeginForm())
{
<table id="images">
<tbody>
@foreach (var item in Model.Pictures.Where(o => o.PartnerId == Model.PartnerId && !o.IsDeleted))
{
<tr>
<td>
<input name="chkPicture" type="checkbox" class="check" value="@item.Id"/>
</td>
<td>
<img src="@(Model.BaseUrl)GetPreview.ashx?h=200&id=@item.Id&w=200"/>
</td>
</tr>
}
</tbody>
</table>
<div id="container">
@Html.ActionLink(R("Remove"), "DeleteSelectedPictures")
</div>
}
</div>
and cs controller:
public ActionResult DeleteSelectedPictures()
{
var selected = Request.Form["chkPicture"];
var selectedList = selected.Split(',');
foreach (var temp in selectedList)
{
var strTemp = Convert.ToInt32(temp);
var deletePicture = _Db.Pictures.FirstOrDefault(p => p.Id == strTemp);
_Db.Pictures.Remove(deletePicture);
_Db.SaveChanges();
}
return RedirectToAction("Index");
}