Introduction
In this article, I am going to explain how to perform CRUD operations in ASP.NET MVC using AngularJS and WEB API 2 With Stored Procedure.
So, you should follow the below steps to create a web application.
Step 1
New, create a database table and you can follow this query.
- CREATE TABLE [dbo].[Employee] (
- [Id] INT IDENTITY (1, 1) NOT NULL,
- [Name] NVARCHAR (50) NOT NULL,
- [Address] NVARCHAR (50) NOT NULL,
- [Country] NVARCHAR (50) NOT NULL,
- [City] NVARCHAR (50) NOT NULL,
- [Mobile] NVARCHAR (10) NOT NULL,
- PRIMARY KEY CLUSTERED ([Id] ASC)
- );
Step 2
Create a Stored Procedure.
- CREATE PROCEDURE sp_InsUpdDelEmployee
- @id INT ,
- @name NVARCHAR(50) ,
- @address NVARCHAR(50) ,
- @country NVARCHAR(50) ,
- @city NVARCHAR(50) ,
- @mobile NVARCHAR(50) ,
- @type VARCHAR(10)
- AS
- BEGIN
- IF ( @type = 'Ins' )
- BEGIN
- INSERT INTO Employee
- VALUES ( @name, @address, @country, @city, @mobile )
- END
- IF ( @type = 'Upd' )
- BEGIN
- UPDATE Employee
- SET Name = @name ,
- [Address] = @address ,
- Country = @country ,
- City = @city ,
- Mobile = @mobile
- WHERE Id = @id
- END
- IF ( @type = 'Del' )
- BEGIN
- DELETE FROM Employee
- WHERE Id = @id
- END
- IF ( @type = 'GetById' )
- BEGIN
- SELECT *
- FROM Employee
- WHERE Id = @id
- END
- SELECT *
- FROM Employee
- END
Step 3
Open Visual Studio and click File > New > Project.

Step 4
Click on Web from the left-side panel and select ASP.NET MVC4 Web Application. Give it a name and click OK.
Step 5
Select "INTERNET APPLICATION".
Step 6
Right-click on Controller and click Add >> Controller.
Step 7
Give a name to your controller and select Empty API Controller.
Step 8
Right-click on App_Data folder and select Add > New Item.
Step 9
Select SQL Server database and give it a name and Click OK.
Step 10
Right-click on Models and click Add > New Item.

Step 11
Select ADO.NET Entity Data Model.
Step 12
Select "Generate from database" and click "Next".
Step 13
Choose the appropriate database connection and click "Next".
Step 14
Select the created table and created Stored Procedure.
Step 15
Write the following code in Model.
- namespace AngularJs_With_Web_API.Models
- {
- using System;
- using System.Collections.Generic;
- public partial class Employee
- {
- public int Id { get; set; }
- public string Name { get; set; }
- public string Address { get; set; }
- public string Country { get; set; }
- public string City { get; set; }
- public string Mobile { get; set; }
- }
- }
Step 16
Now, write the following code in BundleConfig.cs file.
- using System.Web;
- using System.Web.Optimization;
- namespace AngularJs_With_Web_API
- {
- public class BundleConfig
- {
- // For more information on Bundling, visit http://go.microsoft.com/fwlink/?LinkId=254725
- public static void RegisterBundles(BundleCollection bundles)
- {
- bundles.Add(new ScriptBundle("~/js").Include(
- "~/js/angular.js",
- "~/js/app.js"));
- bundles.Add(new StyleBundle("~/css").Include(
- "~/css/bootstrap.css"));
- }
- }
- }
Step 17
Now, if talking about View, then write the following code in Index.cshtml file.
- @Scripts.Render("~/js")
- @Styles.Render("~/css")
- <html ng-app="myApp">
- <head><title>AngularJs With WebApi and Stored Procedure</title></head>
- <body>
- <div ng-controller="employeeController" class="container">
- <div class="row">
- <div class="col-md-12">
- <h3 class="header">AngularJs With WebApi and Stored Procedure</h3>
- </div>
- </div>
- <div class="row">
- <div class="col-md-12">
- <strong class="error">{{error}}</strong>
- <form name="addemployee" style="width: 600px; margin: 0px auto;">
- <div class="form-group">
- <label for="cname" class="col-sm-2 control-label">Name:</label>
- <div class="col-sm-10 space">
- <input type="text" class="form-control" id="cname" placeholder="please enter your name" ng-model="newemployee.Name" required />
- </div>
- </div>
- <div class="form-group">
- <label for="address" class="col-sm-2 control-label">Address:</label>
- <div class="col-sm-10 space">
- <textarea class="form-control" id="address" placeholder="please enter your address" ng-model="newemployee.Address" required></textarea>
- </div>
- </div>
- <div class="form-group">
- <label for="country" class="col-sm-2 control-label">Country:</label>
- <div class="col-sm-10 space">
- <input type="text" class="form-control" id="country" placeholder="please enter your country" ng-model="newemployee.Country" required />
- </div>
- </div>
- <div class="form-group">
- <label for="city" class="col-sm-2 control-label">City:</label>
- <div class="col-sm-10 space">
- <input type="text" class="form-control" id="city" placeholder="please enter your city" ng-model="newemployee.City" required />
- </div>
- </div>
- <div class="form-group">
- <label for="mobile" class="col-sm-2 control-label">Mobile:</label>
- <div class="col-sm-10 space">
- <input type="text" class="form-control" id="mobile" placeholder="please enter your mobile" ng-model="newemployee.Mobile" required />
- </div>
- </div>
- <br />
- <div class="form-group space">
- <div class="col-sm-offset-2 col-sm-10">
- <input type="submit" value="Add" ng-click="add()" ng-show="addShow" ng-disabled="!addemployee.$valid" class="btn btn-primary" />
- <input type="submit" value="Update" ng-click="update()" ng-show="updateShow" ng-disabled="!addemployee.$valid" class="btn btn-primary" />
- <input type="button" value="Cancel" ng-click="cancel()" class="btn btn-primary" />
- </div>
- </div>
- <br />
- </form>
- </div>
- </div>
- <div class="row">
- <div class="col-md-12">
- <div class="table-responsive">
- <table class="table table-bordered table-hover" style="width: 800px; margin-left: 170px;">
- <tr>
- <th>Name</th>
- <th>Address</th>
- <th>Country</th>
- <th>City</th>
- <th>Mobile</th>
- <th>Actions</th>
- </tr>
- <tr ng-repeat="employee in employees">
- <td>
- <p>{{ employee.Name }}</p>
- </td>
- <td>
- <p>{{ employee.Address }}</p>
- </td>
- <td>
- <p>{{ employee.Country }}</p>
- </td>
- <td>
- <p>{{ employee.City }}</p>
- </td>
- <td>
- <p>{{ employee.Mobile }}</p>
- </td>
- <td>
- <p><a ng-click="edit()" href="javascript:void(0);">Edit</a> | <a ng-click="delete()" href="javascript:void(0);">Delete</a></p>
- </td>
- </tr>
- </table>
- </div>
- </div>
- </div>
- </div>
- </body>
- </html>

Join the conversation! Your thoughts help the community grow.