How to Create CRUD Operation in ASP.NET MVC using WEB API

Step 1: Open visual studio-2012 select MVC template and select WEB API tab.
Step 2: Create database.
Step 3: Create Model class through Entity Framework and Build that class.
Step 4: Generate bydefault HomeController and ValueController.
 
ValueController.cs
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Net;  
  5. using System.Net.Http;  
  6. using System.Web.Http;  
  7. using WebAPIExample.Models;  
  8. using System.Data.Entity;  
  9. namespace WebAPIExample.Controllers  
  10. {  
  11.     public class ValuesController: ApiController  
  12.     {  
  13.         BMGEntities1 db = new BMGEntities1();  
  14.         // GET api/values  
  15.         public IEnumerable < string > Get()   
  16.         {  
  17.             return new string[] {  
  18.                 "value1""value2"  
  19.             };  
  20.         }  
  21.         // GET api/values/5  
  22.         [HttpGet]  
  23.         public EmpRecord GetEmp(int id)   
  24.         {  
  25.             EmpRecord obj = db.EmpRecords.Find(id);  
  26.             return obj;  
  27.         }  
  28.         // POST api/values  
  29.         [HttpPost]  
  30.         public string AddEmp(EmpRecord obj)   
  31.         {  
  32.             db.EmpRecords.Add(obj);  
  33.             db.SaveChanges();  
  34.             return "record inserted successfully....";  
  35.         }  
  36.         // PUT api/values/5  
  37.         [HttpPut]  
  38.         public string EditEmp(EmpRecord obj)   
  39.         {  
  40.             EmpRecord obj2 = db.EmpRecords.SingleOrDefault(e1 = > e1.EId == obj.EId);  
  41.             obj2.Ename = obj.Ename;  
  42.             obj2.Sal = obj.Sal;  
  43.             db.SaveChanges();  
  44.             return "record updated successfully.......";  
  45.         }  
  46.         // DELETE api/values/5  
  47.         [HttpDelete]  
  48.         public string DeleteEmp(int id)  
  49.         {  
  50.             EmpRecord obj = db.EmpRecords.Find(id);  
  51.             db.EmpRecords.Remove(obj);  
  52.             db.SaveChanges();  
  53.             return "record deleted successfully......";  
  54.         }  
  55.     }  
  56. }
HomeController.cs
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.Mvc;  
  6. namespace WebAPIExample.Controllers   
  7. {  
  8.     public class HomeController: Controller   
  9.     {  
  10.         public ActionResult Index()   
  11.         {  
  12.             return View();  
  13.         }  
  14.     }  
  15. }
Step 5: Create view for index actionmethod.
 
Index.cshtml
  1. @{  
  2. ViewBag.Title = "Index";  
  3. }  
  4.   
  5. <h2>Web API</h2>  
  6. <script src="~/Scripts/jquery-1.10.2.min.js"></script>  
  7. <script>  
  8. function fn_GetEmp() {  
  9. var eno = $("#Eid").val();  
  10. $.ajax({  
  11. url: "http://localhost:1533/api/Values/" + eno,  
  12. type: "GET",  
  13. contentType: "application/json; charset=utf-8",  
  14. dataType: "JSON",  
  15. success: function (response) {  
  16. $("#Ename").val(response.Ename);  
  17. $("#Sal").val(response.Sal);  
  18. }  
  19. });  
  20. }  
  21. function fn_AddEmp() {  
  22. var empData =  
  23. {  
  24. "Eid": $("#Eid").val(),  
  25. "Ename": $("#Ename").val(),  
  26. "Sal": $("#Sal").val(),  
  27. };  
  28. $.ajax({  
  29. data:JSON.stringify(empData),  
  30. url: "http://localhost:1533/api/Values",  
  31. type: "POST",  
  32. contentType: "application/json; charset=utf-8",  
  33. dataType: "JSON",  
  34. success: function (response) {  
  35. $("#sp1").text("Inserted suceessfully....");  
  36. }  
  37. });  
  38. }  
  39. function fn_EditEmp() {  
  40. var empData =  
  41. {  
  42. "Eid": $("#Eid").val(),  
  43. "Ename": $("#Ename").val(),  
  44. "Sal": $("#Sal").val(),  
  45. };  
  46. Eno= $("#Eid").val(),  
  47. $.ajax({  
  48. data: JSON.stringify(empData),  
  49. url: "http://localhost:1533/api/Values/"+Eno,  
  50. type: "PUT",  
  51. contentType: "application/json; charset=utf-8",  
  52. dataType: "JSON",  
  53. success: function (response) {  
  54. $("#sp1").text("Updated successfully....");  
  55. }  
  56. });  
  57. }  
  58. function fn_DeleteEmp() {  
  59. var Eid = $("#Eid").val();  
  60. $.ajax({  
  61. url: "http://localhost:1533/api/Values/" + Eid,  
  62. type: "Delete",  
  63. contentType: "application/json; charset=utf-8",  
  64. dataType: "JSON",  
  65. success: function (response) {  
  66. $("#sp1").text("Deleted successfully....");  
  67. }  
  68. });  
  69. }  
  1. undefined</script>    
  2. @Html.Label("Eid")    
  3. @Html.TextBox("Eid")    
  4. undefined<br />undefined<br />    
  5. @Html.Label("EName")    
  6. @Html.TextBox("Ename")    
  7. undefined<br />undefined<br />    
  8. @Html.Label("Sal")    
  9. @Html.TextBox("Sal")    
  10. undefined  
  11. <br />undefined  
  12. <br />undefined  
  13. <input type="button" value="GetEmp" onclick="fn_GetEmp()" />undefined  
  14. <input type="button" value="AddEmp" onclick="fn_AddEmp()"/>undefined  
  15. <input type="button" value="EditEmp" onclick="fn_EditEmp()" />undefined  
  16. <input type="button" value="DeleteEmp" onclick="fn_DeleteEmp()" />undefined  
  17. <hr />undefined<span id="sp1">  
  18. </span>  
Step 6: Build and Run the project.