Delete multiple item from gridview in mvc4
Step1: first of all creare a table ex=tbemp fields are (eid,ename,esal,eadd)
Step:2 Create a mvc4 project,select internet application from template pane then select aspx view
engine.
Step 3:Right click on your model folder and add linq to sql and name it Employee.dbml.
Step 4:In Home Controller write following code.
public ActionResult Employee()
{
EmployeeDataContext emp = new EmployeeDataContext();
var abc = from p in emp.tbemps select p;
return View(abc);
}
step 5:Right click on above action method and the add view name it Employee.
step 6:write following code in employee.aspx
=================================================
<%@ Page Title=”" Language=”C#” MasterPageFile=”~/Views/Shared/Site.Master” Inherits=”System.Web.Mvc.ViewPage<IEnumerable<checkboxingrid.Models.tbemp>>” %>
<asp:Content ID=”Content1? ContentPlaceHolderID=”TitleContent” runat=”server”>
Employee
</asp:Content>
<asp:Content ID=”Content2? ContentPlaceHolderID=”MainContent” runat=”server”>
<script type=”text/javascript”>
checked = false;
function checkedAll()
{
if (checked == false)
{
checked = true
}
else
{
checked = false
}
for (var i = 0; i < document.getElementById(‘myform').elements.length; i++)
{
document.getElementById(‘myform').elements[i].checked =checked;
}
}
</script>
<%using (Html.BeginForm(“Employee”,”Home”,FormMethod.Post, new { id = “myform” }))
{
%>
<div>
<table style=”width:500px;”>
<tr>
<th>
<input type=”checkbox” onclick=”checkedAll()” name=”kk” id=”kk” />
</th>
<th>EmpID</th>
<th>Emp Name</th>
<th>Salary</th>
<th>Address</th>
</tr>
<%foreach (var item in Model)
{ %>
<tr>
<td align=”center”>
<%=Html.CheckBox(“cb” + item.eid)%>
</td>
<td align=”center”>
<%:item.eid%>
</td>
<td align=”center”>
<%:item.ename%>
</td>
<td align=”center”>
<%:item.esal%>
</td>
<td align=”center”>
<%:item.eadd%>
</td>
</tr>
<%} %>
</table>
</div>
<div><input type=”submit” value=”Delete” /></div>
<%} %>
</asp:Content>
<asp:Content ID=”Content3? ContentPlaceHolderID=”FeaturedContent” runat=”server”>
</asp:Content>
==================================================================
Step 7:for getting selected value of check box write code in home controller.
public ActionResult Employee(tbemp emp2)
{
string st = “”;
foreach (string key in Request.Form)
{
var checkbox =”";
if (key.StartsWith(“cb”))
{
checkbox = Request.Form["" +key];
if (checkbox != “false”)
{
Int32 id = Convert.ToInt32(key.Remove(0, 2));
st += id.ToString() + “,”;
}
}
}
EmployeeDataContext emp = new EmployeeDataContext();
var abc = from p in emp.tbemps select p;
return Content(“the id selected for delete is=” + st);
// return RedirectToAction(“Employee”, “Home”, abc);
//return View(abc);
}