This article will explain how to perfrom CRUD operation in ASP.Net MVC application.
Objective:The core purpose of this article is to demonstrate how to perform CRUD operation on a database in ASP.NET MVC Framework.See my other articles on ASP.NET MVC Framework to get an understanding of different components of framework. My articles could be found atIntroduction HereOutput Caching HereAbout Controller hereSilverlight in MVC Framework hereFinal output would be something likeData Base Description
Note: Database script is being attached to download. So either you could run that database script or you could create the database manually by yourself. Creating MVC application
Creating the Data Model
Creating the Home Controller
namespace AuthorContactDetail.Controllers{ public class HomeController : Controller { // // GET: /Home/
public ActionResult Index() { return View(); }
// // GET: /Home/Details/5
public ActionResult Details(int id) { return View(); }
// // GET: /Home/Create
public ActionResult Create() { return View(); }
// // POST: /Home/Create
[AcceptVerbs(HttpVerbs.Post)] public ActionResult Create(FormCollection collection) { try { // TODO: Add insert logic here
return RedirectToAction("Index"); } catch { return View(); } }
// // GET: /Home/Edit/5 public ActionResult Edit(int id) { return View(); }
// // POST: /Home/Edit/5
[AcceptVerbs(HttpVerbs.Post)] public ActionResult Edit(int id, FormCollection collection) { try { // TODO: Add update logic here
return RedirectToAction("Index"); } catch { return View(); } } }}Note: _entities is initialized globally.
<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server"> Index</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<h2>Index</h2>
<table> <tr> <th></th> <th> Id </th> <th> FirstName </th> <th> LastName </th> <th> Phone </th> <th> Email </th> </tr>
<% foreach (var item in Model) { %>
<tr> <td> <%= Html.ActionLink("Edit", "Edit", new { id=item.Id }) %> | <%-- <%= Html.ActionLink("Details", "Details", new { id=item.Id })%>--%> </td> <td> <%= Html.Encode(item.Id) %> </td> <td> <%= Html.Encode(item.FirstName) %> </td> <td> <%= Html.Encode(item.LastName) %> </td> <td> <%= Html.Encode(item.Phone) %> </td> <td> <%= Html.Encode(item.Email) %> </td> </tr>
<% } %> </table>
<p> <%= Html.ActionLink("Create New", "Create") %> </p>
</asp:Content>Note: Here, Details Link is commented, because we don't have any details of record. Just press F5 to run with debugging.
Creating New ContactsIf you see at Controller\HomeController.cs, for creating new contacts you will find code below. // GET: /Home/Create
// // GET: /Home/Edit/5
[AcceptVerbs(HttpVerbs.Post)] public ActionResult Create([Bind(Exclude="Id")] Author_Contact_Details contactDetail) {
if (!ModelState.IsValid) return View(); try { // TODO: Add insert logic here _entities.AddToAuthor_Contact_Details(contactDetail); _entities.SaveChanges();
<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server"> Create</asp:Content>
<h2>Create</h2>
<%= Html.ValidationSummary("Create was unsuccessful. Please correct the errors and try again.") %>
<% using (Html.BeginForm()) {%> <fieldset> <legend>Fields</legend> <p> <label for="Id">Id:</label> <%= Html.TextBox("Id") %> <%= Html.ValidationMessage("Id", "*") %> </p> <p> <label for="FirstName">FirstName:</label> <%= Html.TextBox("FirstName") %> <%= Html.ValidationMessage("FirstName", "*") %> </p> <p> <label for="LastName">LastName:</label> <%= Html.TextBox("LastName") %> <%= Html.ValidationMessage("LastName", "*") %> </p> <p> <label for="Phone">Phone:</label> <%= Html.TextBox("Phone") %> <%= Html.ValidationMessage("Phone", "*") %> </p> <p> <label for="Email">Email:</label> <%= Html.TextBox("Email") %> <%= Html.ValidationMessage("Email", "*") %> </p> <p> <input type="submit" value="Create" /> </p> </fieldset>
<% } %>
<div> <%=Html.ActionLink("Back to List", "Index") %> </div>
</asp:Content>Just press F5 to run with debugging.
Editing ContactsIf you see at Controller\HomeController.cs , for editing contacts you will find code below. // GET: /Home/Edit/5
public ActionResult Edit(int id) { return View(); }
public ActionResult Edit(int id) {
var res = (from r in _entities.Author_Contact_Details where r.Id == id select r).FirstOrDefault(); return View(res); }
[AcceptVerbs(HttpVerbs.Post)] public ActionResult Edit(Author_Contact_Details contactDetail) {
if (!ModelState.IsValid) return View(); try { // TODO: Add update logic here var res = (from r in _entities.Author_Contact_Details where r.Id == contactDetail.Id select r).FirstOrDefault(); _entities.ApplyPropertyChanges(res.EntityKey.EntitySetName, contactDetail); _entities.SaveChanges();
Deleting ContactsController\HomeController.cspublic ActionResult Delete(int Id) { var res = (from r in _entities.Author_Contact_Details where r.Id == Id select r).FirstOrDefault(); return View(res); }
[AcceptVerbs(HttpVerbs.Post)] public ActionResult Delete(Author_Contact_Details contactDetail) { try { var res = (from r in _entities.Author_Contact_Details where r.Id == contactDetail.Id select r).FirstOrDefault(); _entities.DeleteObject(res); _entities.SaveChanges(); return RedirectToAction("Index");
} catch { return View(); }
}
<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server"> Index/asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server"> <h2>Index</h2>
<% foreach (var item in Model) { %> <tr> <td> <%= Html.ActionLink("Edit", "Edit", new { id=item.Id }) %> | <%= Html.ActionLink("Delete", "Delete", new { id=item.Id })%> </td> <td> <%= Html.Encode(item.Id) %> </td> <td> <%= Html.Encode(item.FirstName) %> </td> <td> <%= Html.Encode(item.LastName) %> </td> <td> <%= Html.Encode(item.Phone) %> </td> <td> <%= Html.Encode(item.Email) %> </td> </tr>
</table>
</asp:Content>
Changing the caption of the site
Summary:This article explained about, how to perform CRUD operation on a table in ASP.Net MVC Framework.Future Scope:I will modify sample in this article to accommodate authentication, Ajax etc.
ASP.NET MVC 5 Recipes: A Practical Solution