24 May 2017

How to delete multiple webgrid rows by using Checkboxes in asp.net MVC Application

How to delete multiple webgrid rows by using Checkboxes in asp.net MVC  Application

Create sample Student table using below script
CREATE TABLE [dbo].[Student](
[Id] [int] NOT NULL,
[Name] [varchar](50) NULL,
[Class] [int] NULL,
[Fees] [int] NULL,
CONSTRAINT [PK_Student] PRIMARY KEY CLUSTERED
(
[Id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

GO
SET ANSI_PADDING OFF
GO
INSERT [dbo].[Student] ([Id], [Name], [Class], [Fees]) VALUES (1, N'John', 3, 15000)
GO
INSERT [dbo].[Student] ([Id], [Name], [Class], [Fees]) VALUES (2, N'David', 3, 15000)
GO
INSERT [dbo].[Student] ([Id], [Name], [Class], [Fees]) VALUES (3, N'Mark', 5, 25000)
GO
INSERT [dbo].[Student] ([Id], [Name], [Class], [Fees]) VALUES (4, N'Juda', 7, 30000)
GO
INSERT [dbo].[Student] ([Id], [Name], [Class], [Fees]) VALUES (5, N'Reuben', 9, 50000)
GO
==========================================================================

Right click on solution => Add New Item => Data => ADO.NET Entity Data Model
=> Name it MyModel.edmx => Add => Generate from Database =>
next => New Connection => Provide Server details and select Database
=> Test Connection => Rename Entity Connection => MyDatabaseEntities => next => Select your table => Finish.


Right click on Controllers => Add Controller StudentController=> Add.

Build Solution.

public class StudentController : Controller
{
//
// GET: /Student/

public ActionResult List()
{
List<Student> allStudents = new List<Student>();
using (MyDatabaseEntities dc = new MyDatabaseEntities())
{
allStudents= dc.Students.ToList();
}
return View(allStudents);
}
public ActionResult DeleteSelected(string[] ids)
{
int[] id = null;
if(ids != null)
{
id = new int[ids.Length];
int j = 0;
foreach( string i in ids)
{
int.TryParse(i, out id[j++]);
}

}
if(id!= null && id.Length > 0)
{
List<Student> AllSelected = new List<Student>();
using (MyDatabaseEntities dc= new MyDatabaseEntities())
{
AllSelected = dc.Students.Where(a => id.Contains(a.Id)).ToList();
foreach(var i in AllSelected)
{
dc.Students.Remove(i);
}
dc.SaveChanges();
}
}
return RedirectToAction("List");
}

}
==================================================================================================
Add View => List and Check Strongly typed view , model Class set to Student, Scaffold template is List => Add
@model IEnumerable<Multi_Delete_grid_rows_using_Checkboxes_in_asp.net_MVC.Models.Student>

@{
ViewBag.Title = "List";
var grid = new WebGrid(source: Model, rowsPerPage: 10);
}
<style>
table.gridtable {
font-family: Verdana, Arial, sans-serif;
font-size: 12px;
color: #333333;
border-width: 1px;
border-color: #666666;
border-collapse: collapse;
}
table.gridtable th{
border-width:1px;
padding : 8px;
border-style:solid;
border-color: #666666;
background-color:#dedede;
}
table.gridtable td {
border-width: 1px;
padding : 8px;
border-style: solid;
border-color:#666666;
background-color:#ffffff;


}

</style>
<h2>List</h2>

@using (Html.BeginForm("DeleteSelected", "Student", FormMethod.Post))
{
@grid.GetHtml(
tableStyle:"gridtable",
columns:
grid.Columns(
grid.Column(format:@<text><input type="checkbox" name="ids" value="@item.Id"/></text>, header: "Select"),
grid.Column("Id", "Id"),
grid.Column("Name", "Name"),
grid.Column("Class", "Class"),
grid.Column("Fees", "Fees")
)
)
<input type="submit" value="Delete Selected" />
}

=======================================================================
Set your default page in Route config file
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Student", action = "List", id = UrlParameter.Optional }
);
==================================================================================================

No comments:

Post a Comment