Create, Edit And Delete Operation Of Data In ASP.NET MVC

This is a simple application for beginners that helps with to how to perform the create, edit and delete operation of data in an ASP.NET MVC application.

Introduction

This is a simple application for beginners that helps with how to perform create,edit,delete operations on a table in an ASP.NET MVC application. It also helps with how to create the database and add a table. We know that MVC is an integrated module. This is the integration of three words models,views,controllers. Models provide the business logic, Views isprovide the presentation and last controllers handle all of the requests that are provided by models,views. ASP.NET MVC gives you a powerful patterns-based way to build dynamic websites that enables a clean separation of concerns and that gives you full control over markup for enjoyable agile development. ASP.NET MVC is an alternative but not a replacement for ASP.NET Web Forms.

Step 1

Open Visual Studio 2010.

  • Click file ->New->Projects
  • Add MVC ASP. NET 2 Web application
  • The name of application is "bhanu1"
  • Create unit test

starrrrrrrrrrrrrrrrrrrrrr.gif

createunittest.gif

Step 2

In the App_Data folder add SQL SERVER DATABASE. SQL SERVER DATABASE will then takes care of generating the appropriate SQL execution logic to use at runtime when we interact and use them.

  • Right Click of App_Data Folder->Add New Item-> add "SQL SERVER DATABASE" class
  • Name of database is "Person"

add-database.gif

aaaaaaaaaaaaaaaaaaaaaaa.gif

Step 3

After add ingthe SQL SERVER DATABASE two files are created in the folder.

  • First is "Person.mdf"
  • Second is "person.ldf"

Step 4

Open the Server Explorer.

  • Right Click on "person.mdf"->Add Table Schema
  • Set the fields "Id,Name,salary,Address"

opensqlserver.gif

addtable.gif

settableschemaandprimarykey.gif

savetable.gif

tablename.gif

Step 5

Click the table.

  • Right click on sandeep and open "Show table data"
  • Insert data from a table

showtabledata.gif

entertabledata.gif

Step 6

Add "ADO.NET Entity Data Model".

  • Right Click on the Model Folder->add->add new item->ADO.NET Entity Data Model
  • Bind the data

addadoclas.gif


adoentitymodel.gif

databseadd.gif

entitydatamodel.gif

retrivingdatabaseinformatio.gif

Step 7

Add controller.

  • Right Click on the controller folder->add->controller
  • Name of the controller "ranu contoller"
  • Click the checkbox to perform all action

indexview.gif

createview.gif

deleteview.gif

editview.gif

detailview.gif

Code

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.Mvc;  
  6. using bhanu1.Models;  
  7. namespace bhanu1.Controllers  
  8. {  
  9.     public class ranuController : Controller  
  10.     {  
  11.         //  
  12.         // GET: /ranu/  
  13.         private personEntities1 per = new personEntities1();  
  14.         public ActionResult Index()  
  15.         {  
  16.             return View(per.sandeep1.ToList());  
  17.         }  
  18.         //  
  19.         // GET: /ranu/Details/5  
  20.         public ActionResult Details(int id)  
  21.         {  
  22.             return View();  
  23.         }  
  24.         //  
  25.         // GET: /ranu/Create  
  26.         public ActionResult Create()  
  27.         {  
  28.             return View();  
  29.         }   
  30.         //  
  31.         // POST: /ranu/Create  
  32.         [HttpPost]  
  33.         public ActionResult Create([Bind(Exclude = "Id")]sandeep1 san)  
  34.         {  
  35.             try  
  36.             {  
  37.                 // TODO: Add insert logic here  
  38.                 per.AddTosandeep1(san);  
  39.                 per.SaveChanges();  
  40.                 return RedirectToAction("Index");  
  41.             }  
  42.             catch  
  43.             {  
  44.                 return View();  
  45.             }  
  46.         }  
  47.         //  
  48.         // GET: /ranu/Edit/5  
  49.         public ActionResult Edit(int id)  
  50.         {  
  51.             return View();  
  52.         }  
  53.         //  
  54.         // POST: /ranu/Edit/5  
  55.         [HttpPost]  
  56.         public ActionResult Edit(int id, FormCollection collection)  
  57.         {  
  58.             try  
  59.             {  
  60.                 // TODO: Add update logic here  
  61.                 return RedirectToAction("Index");  
  62.             }  
  63.             catch  
  64.             {  
  65.                 return View();  
  66.             }  
  67.         }  
  68.         //  
  69.         // GET: /ranu/Delete/5  
  70.         public ActionResult Delete(int id)  
  71.         {  
  72.             return View();  
  73.         }  
  74.         //  
  75.         // POST: /ranu/Delete/5  
  76.         [HttpPost]  
  77.         public ActionResult Delete(int id, FormCollection collection)  
  78.         {  
  79.             try  
  80.             {  
  81.                 // TODO: Add delete logic here  
  82.                 return RedirectToAction("Index");  
  83.             }  
  84.             catch  
  85.            {  
  86.                 return View();  
  87.             }  
  88.         }  
  89.     }  

Step 8

Add views.

  • first add a view "index"
  • second add a view "create"
  • third view is "delete"
  • four view is "detail"
  • six view is "edit"

Code

Code of index views

  1. <%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<IEnumerable<bhanu1.Models.sandeep1>>" %>  
  2. <asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">  
  3.        Index  
  4. </asp:Content>  
  5. <asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">  
  6.     <h2 style="background-color: #FFCCCC">Index</h2>  
  7.     <table>  
  8.         <tr>  
  9.             <th></th>  
  10.             <th style="background-color: #3399FF">  
  11.                 Id  
  12.             </th>  
  13.             <th style="background-color: #9999FF">  
  14.                 Name  
  15.             </th>  
  16.             <th style="background-color: #6699FF">  
  17.                 Salary  
  18.             </th>  
  19.             <th style="background-color: #9999FF">  
  20.                 Address  
  21.             </th>  
  22.         </tr>  
  23.     <% foreach (var item in Model) { %>  
  24.         <tr>  
  25.             <td>  
  26.                 <%: Html.ActionLink("Edit""Edit"new { id=item.Id }) %> |  
  27.                 <%: Html.ActionLink("Details""Details"new { id=item.Id })%> |  
  28.                 <%: Html.ActionLink("Delete""Delete"new { id=item.Id })%>  
  29.             </td>  
  30.             <td>  
  31.                 <%: item.Id %>  
  32.             </td>  
  33.             <td>  
  34.                 <%: item.Name %>  
  35.             </td>  
  36.             <td>  
  37.                 <%: item.Salary %>  
  38.             </td>  
  39.             <td>  
  40.                 <%: item.Address %>  
  41.             </td>  
  42.         </tr>  
  43.     <% } %>  
  44.     </table>  
  45.     <p>  
  46.         <%: Html.ActionLink("Create New""Create") %>  
  47.     </p>  
  48. </asp:Content>  

Code

code of create views

  1. <%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<bhanu1.Models.sandeep1>" %>  
  2. <script runat="server">  
  3. </script>  
  4. <asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">  
  5.        Create  
  6. </asp:Content>  
  7. <asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">  
  8.     <h2 style="background-color: #00FFFF">Create</h2>  
  9.     <% using (Html.BeginForm()) {%>  
  10.         <%: Html.ValidationSummary(true) %>  
  11.         <fieldset>  
  12.             <legend style="background-color: #33CC33">Fields</legend>  
  13.             <div class="editor-label">  
  14.                 <%: Html.LabelFor(model => model.Id) %>  
  15.             </div>  
  16.             <div class="editor-field">  
  17.                 <%: Html.TextBoxFor(model => model.Id) %>  
  18.                 <%: Html.ValidationMessageFor(model => model.Id) %>  
  19.             </div>  
  20.             <div class="editor-label">  
  21.                 <%: Html.LabelFor(model => model.Name) %>  
  22.             </div>  
  23.             <div class="editor-field">  
  24.                 <%: Html.TextBoxFor(model => model.Name) %>  
  25.                 <%: Html.ValidationMessageFor(model => model.Name) %>  
  26.             </div>  
  27.             <div class="editor-label">  
  28.                 <%: Html.LabelFor(model => model.Salary) %>  
  29.             </div>  
  30.             <div class="editor-field">  
  31.                 <%: Html.TextBoxFor(model => model.Salary) %>  
  32.                 <%: Html.ValidationMessageFor(model => model.Salary) %>  
  33.             </div>  
  34.             <div class="editor-label">  
  35.                 <%: Html.LabelFor(model => model.Address) %>  
  36.             </div>  
  37.             <div class="editor-field">  
  38.                 <%: Html.TextBoxFor(model => model.Address) %>  
  39.                 <%: Html.ValidationMessageFor(model => model.Address) %>  
  40.             </div>  
  41.             <p>  
  42.                 <input type="submit" value="Create" style="background-color: #FF99CC" />  
  43.             </p>  
  44.         </fieldset>  
  45.     <% } %>  
  46.     <div>  
  47.         <%: Html.ActionLink("Back to List""Index") %>  
  48.     </div>  
  49. </asp:Content>  

Step 9

Press crlt+f5 and run your application.

Output

mmmmmmmmmmmmmmmmmmmm.gif

bbbbbbbbbbbbbbbbbbbb.gif