Pages

Men

rh

10/27/2014

How to Delete Multiple Rows in MVC4 Application

In this article we will delete multiple rows of a database table. For that we will use a checkbox for selecting rows.

Step 1
Create a new project. Go to "File" -> "New" -> "Project..." then Select ASP.NET MVC4 web application then enter the Application Name then click OK then select Internet Application then select View Engine Razor then click OK.

Step 2
Add a database and create a table in it. Go to Solution Explorer then right-click on the App_Data folder then click Add > New item then select SQL Server Database under Data then enter the database name then click Add. After creating table insert some rows manually.
  1. CREATE TABLE [dbo].[tblstudent] (  
  2.     [Id] INT NOT NULL,  
  3.     [SName] NVARCHAR (50) NULL,  
  4.     [stream] NVARCHAR (50) NULL,  
  5.     [college] NVARCHAR (50) NULL,  
  6.     [Address] NVARCHAR (50) NULL,  
  7.     PRIMARY KEY CLUSTERED ([Id] ASC)  
  8. );  
Step 3
Add an entity data model. Go to Solution Explorer then right-click on the project name in the Solution Explorer then click Add > New item then select ADO.Net Entity Data Model under Data then enter the model name then click Add.

The Entity Data Model Wizard will open as a popup window then select Generate from database then click Next.

Choose your data connection then select your database then click Next then select Tables then enter the Model namespace then click Finish.

Step 4
Add a Controller or use the default Home controller. I used the default Home controller. Add a new action into your controller to delete rows. Here I added a "deleteRow".
  1. studentDBEntities db = new studentDBEntities();  
  2. public ActionResult deleteRow()  
  3. {  
  4.     return View(db.tblstudents.ToList());  
  5. }  
  6. [HttpPost]  
  7. public ActionResult deleteRow(IEnumerable<int> studentRecordDeletebyId)  
  8. {  
  9.     foreach (var id in studentRecordDeletebyId)  
  10.     {  
  11.         var student = db.tblstudents.Single(s => s.Id == id);  
  12.           
  13.         db.tblstudents.Remove(student);  
  14.     }  
  15.     db.SaveChanges();  
  16.     return RedirectToAction("deleteRow");  
  17. }  
Step 5
Add a view for the action. Right-click on the Action Method then click Add View then enter View Name then select View Engine then check "Create a strong-typed view" then select your model class then click Add.
  1. @using (Html.BeginForm("deleteRow""Home", FormMethod.Post))  
  2. {  
  3.     <h2>delete multiple Rows</h2>  
  4.     <table style="background-color:azure" border="1">  
  5.         <thead style="background-color:Background">  
  6.             <tr>  
  7.                 <th>  
  8.                     Select  
  9.                 </th>  
  10.                 <th>  
  11.                     Student Name  
  12.                 </th>  
  13.                 <th>  
  14.                     Stream  
  15.                 </th>  
  16.                 <th>  
  17.                     College  
  18.                 </th>  
  19.                 <th>      
  20.                     Address  
  21.                 </th>  
  22.             </tr>  
  23.         </thead>  
  24.     <tbody>  
  25.     @Html.EditorForModel()  
  26.     </tbody>  
  27.     </table>  
  28.     <input type="submit" value="Delete selected student" />  
  29. }  
We use @Html.EditorForModel() because we created the view described in Step 5; this view will fetch the rows from the database. 

Step 6
Add add another view. Add an EditorTemplates folder inside the shared folder and right-click on the EditorTemplates and add a view, the same name as a tblstudent model. 
  1. <tr>  
  2.     <td>  
  3.         <input type="checkbox" name="studentRecordDeletebyId" id="studentRecordDeletebyId" value="@Model.Id" />  
  4.     </td>  
  5.     <td>  
  6.         @Model.SName  
  7.     </td>  
  8.     <td>  
  9.         @Model.stream  
  10.     </td>  
  11.     <td>  
  12.         @Model.college  
  13.     </td>  
  14.     <td>  
  15.         @Model.Address  
  16.     </td>  
  17. </tr>  
In this view we add an input type checkbox and provide the name studentRecordDeletebyId, the same as we passed as the parameter in our deleteRow action method.

Step 7
Run the application and change the URL. In my case it is http://localhost:50191/Home/deleteRow

Source collected from :
http://www.c-sharpcorner.com/UploadFile/71b340/how-to-delete-multiple-row-in-mvc4-application/ 

No comments :

Post a Comment