-->

Part 1 - How to implement Basic CRUD Functionality with the Entity Framework and ASP.NET MVC4 application



Introduction

In this post, I am going to show you how to implement Basic CRUD Functionality with the Entity Framework and ASP.NET MVC4 application.
I have split the entire application split into following 3 parts for making things more simple and understandable especially for beginners.

Steps :

Step - 1: Create New Project.

Go to File > New > Project > Select asp.net MVC4 web application > Entry Application Name > Click OK > Select Internet Application > Select view engine Razor > OK

Step-2: Add a Database.

Go to Solution Explorer > Right Click on App_Data folder > Add > New item > Select SQL Server Database Under Data > Enter Database name > Add.

Step-3: Create 3 tables for store Country, States, and Contact information.

Open Database > Right Click on Table > Add New Table > Add Columns > Save > Enter table name > Ok.
In this example, I have used 3 tables as below


Contacts

Countries

States

Step-4: Add Entity Data Model.

Go to Solution Explorer > Right Click on Project name form Solution Explorer > Add > New item > Select ADO.net Entity Data Model under data > Enter model name > Add.
A popup window will come (Entity Data Model Wizard) > Select Generate from database > Next >
Chose your data connection > select your database > next > Select tables > enter Model Namespace > Finish.

Step-5: Create a Folder

Here I have created a folder Named "ExtendedClass" in which we will create a class
Go to Solution Explorer > Right Click on Project name from Solution Explorer > Add > New Folder > Rename Folder.

Step-6: Create a (partial) Class for extending 2 fields CountryName and StateName. Here I have also added MetaData Class for Apply Validation on Contact Model.

Here I have created a (partial) class named "Contact"
Go to Solution Explorer > Right Click on the Folder > Add > Class > Enter class name > Add.

[N:B: I have changed the namespace of the class "MVCCRUD.ExtendedClass" to "MVCCRUD" and make it partial class]
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel.DataAnnotations;
  4. using System.Linq;
  5. using System.Web;
  6.  
  7. namespace MVCCRUD
  8. {
  9. [MetadataType(typeof(ContactMetaData))]
  10. public partial class Contact
  11. {
  12. public string CountryName { get; set; }
  13. public string StateName { get; set; }
  14. }
  15.  
  16. public class ContactMetaData
  17. {
  18. [Required(ErrorMessage="Contact Person required",AllowEmptyStrings=false)]
  19. [Display(Name="Contact Person")]
  20. public string ContactPerson { get; set; }
  21.  
  22. [Required(ErrorMessage = "Contact No required", AllowEmptyStrings = false)]
  23. [Display(Name = "Contact No")]
  24. public string ContactNo { get; set; }
  25.  
  26. [Required(ErrorMessage = "Country required")]
  27. [Display(Name = "Country")]
  28. public int CountryID { get; set; }
  29.  
  30. [Required(ErrorMessage = "State required")]
  31. [Display(Name = "State")]
  32. public int StateID { get; set; }
  33. }
  34.  
  35. }

Step-7: Add a new Controller.

Go to Solution Explorer > Right Click on Controllers folder form Solution Explorer > Add > Controller > Enter Controller name > Select Templete "empty MVC Controller"> Add.

Step-8: Add new action into your controller for show data in webgrid.

Here I have used "Index" Action for show data. Please write this following code

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.Mvc;
  6.  
  7. namespace MVCCRUD.Controllers
  8. {
  9. public class ContactsController : Controller
  10. {
  11. //
  12. // GET: /Contacts/
  13.  
  14. public ActionResult Index()
  15. {
  16. List<Contact> allContacts = null;
  17. using (MyDatabaseEntities dc = new MyDatabaseEntities())
  18. {
  19. var v = (from a in dc.Contacts
  20. join b in dc.Countries on a.CountryID equals b.CountryID
  21. join c in dc.States on a.StateID equals c.StateID
  22. select new
  23. {
  24. a,
  25. b.CountryName,
  26. c.StateName
  27. });
  28. if (v != null)
  29. {
  30. allContacts = new List<Contact>();
  31. foreach (var i in v)
  32. {
  33. Contact c = i.a;
  34. c.CountryName = i.CountryName;
  35. c.StateName = i.StateName;
  36. allContacts.Add(c);
  37. }
  38. }
  39. }
  40. return View(allContacts);
  41. }
  42. }
  43. }

Step-9: Add view for the Action & design.

Right Click on Action Method (here right click on form action) > Add View... > Enter View Name > Select View Engine (Razor) > Check "Create a strong-typed view" > Select your model class > Add.
[N:B:Please Rebuild solution before add view.]
Complete View
  1. @model IEnumerable<MVCCRUD.Contact>
  2.  
  3. @{
  4. ViewBag.Title = "Index";
  5. var grid = new WebGrid(source: Model, canPage: true, rowsPerPage: 10);
  6. }
  7.  
  8. <h2>Contact List</h2>
  9. @Html.ActionLink("Add New Contact", "Save", "Home", null, new{ @style="font-size:20px"})
  10. @grid.GetHtml(
  11. tableStyle:"table table-responsive",
  12. columns:grid.Columns
  13. (
  14. grid.Column(columnName:"ContactPerson", header:"Contact Person"),
  15. grid.Column(columnName:"ContactNo", header:"Contact No"),
  16. grid.Column(columnName:"CountryName", header:"Country"),
  17. grid.Column(columnName:"StateName", header:"State"),
  18. grid.Column(header:"Edit", format:@<text>@Html.ActionLink("Edit", "Save", "Home", new { id= item.ContactID}, null)</text>),
  19. grid.Column(header:"Delete", format:@<text>@Html.ActionLink("Delete", "Delete", "Home", new { id= item.ContactID}, null)</text>)
  20. )
  21. )
Optional: Here I have added Bootstrap css in the layout page for Responsive design.

Step-10: Run Application.

Download  Live Demo




Hello ! My name is Sourav Mondal. I am a software developer working in Microsoft .NET technologies since 2010.

I like to share my working experience, research and knowledge through my site.

I love developing applications in Microsoft Technologies including Asp.Net webforms, mvc, winforms, c#.net, sql server, entity framework, Ajax, Jquery, web api, web service and more.

Related Posts: