In this article, I am going to explain you how to perform CRUD operation (Create, Read, Update and Delete) using AngularJS with ASP.NET MVC. We will be using Visual Studio 2013.
Creating Database Table
We will be using tblEmployee table for database operation. Use below script to create table.
update
tblEmployee set Name=@Name,Gender=@Gender,Salary=@Salary where ID=@ID
end
end
elseif(@task='Delete')
begin
deletefrom tblEmployee where
ID=@ID
end
end
Creating ASP.NET MVC Empty Application
Now the next step is to create ASP.NET MVC empty application as shown below.
Go to File → New → Project. A new window will be open as shown below.
Now go to Web and select .NET Framework 4.5 and give project name and click on OK .
Now new window will open as shown below.
Now Select Empty Template, check on MVC checkbox and click on OK.
Now, a new project will be created as shown below.
Adding Models
Go to Models folder and add new class file Employee.cs. Now replace all code with below code.
using System;
using
System.Collections.Generic;
using System.Linq;
using System.Web;
namespace
WebApplication1.Models
{
publicclassEmployee
{
publicint? ID { get; set; }
publicstring Name { get; set; }
publicstring Gender { get; set; }
publicstring Salary { get; set; }
}
}
Adding Data Access Layer
Create a Repository.cs class file in root directory of application. Add below code. Here, we have three methods GetAllEmployees() to fetch employee records, SaveEmployee() to save employee details and DeleteEmployee() to delete employee record.
using System;
using
System.Collections.Generic;
using
System.Configuration;
using System.Data;
using
System.Data.SqlClient;
using System.Linq;
using System.Web;
using
WebApplication1.Models;
namespace WebApplication1
{
publicclassRepository
{
publicIEnumerable<Employee
>
GetAllEmployees()
{
// fetching connection string from web.config file
// you can also use Entity Framework or Dapper ORM to
fetch data from database
List<Employee> empList = null;
try
{
using (SqlConnection con = newSqlConnection(ConfigurationManager.ConnectionStrings["DBCS"].ToString()))
Next step is to add controller to application. Go to controller folder and add new empty controller named as Home controller. Replace all code with below code.
Now, right click on home controller index action method, add new view and name Index. Select Empty template and uncheck Use layout page and click on Add as shown below.
Now go to Views / Home folder, open Index.cshtml file and replace all code with below code. We need to download all the js and css file from internet.
Index.cshtml Code:
@{
ViewBag.Title = "Angular CURD operation in ASP.NET MVC";
}
@*
required angular and
jquery js file
*@
<scriptsrc="~/Scripts/jquery-3.3.1.js"></script>
<scriptsrc="~/Scripts/angular.js"></script>
@*
custom javascript file
where we write angular code
*@
Now, we need to write custom javascript file myJavaScripts.js and save in Scripts folder. Now drag and drop Angular.js and jQuery.js files at the top of custom javascript file for the reference as shown below.
/// <reference path="angular.js" />
/// <reference path="jquery-3.3.1.js" />
var app = angular.module("myApp", []);
app.controller("myCtrl", function ($scope, $http) {
$scope.GetAllEmployees =
function () {
$http({
method: "GET",
url: "../Home/GetAllEmployees"
}).then(function (response) {
$scope.employees = response.data;
}, function () {
alert("Error occured!!!");
})
};
$scope.SaveEmployee =
function () {
if ($('#addName').val() == "") {
alert('Enter Name');
returnfalse;
}
if ($('#addGender'
).val()
==
"") {
alert('Select Gender');
returnfalse;
}
var EmployeeModel = {
ID:
$scope.ID,
Name:
$scope.Name,
Gender:
$scope.Gender,
Salary:
$scope.Salary,
};
$http({
method: "Post",
datatype:
"json",
data:
JSON.stringify(EmployeeModel),
url: "../Home/SaveEmployee"
}).then(function (response) {
if (response.data.success == true) {
alert('Saved successfully!!!!');
$scope.ID
=
"";
$scope.Name =
"";
$scope.Gender =
"";
$scope.Salary =
"";
$("#addEmployee").modal("toggle");
$scope.GetAllEmployees();
}
else {
alert(
'Save failed!!!');
}
}, function () {
alert("Error occured!!!");
})
}
$scope.EditEmployee =
function (emp) {
$scope.ID =
emp.ID;
$scope.Name
= emp.Name;
$scope.Gender = emp.Gender;
$scope.Salary = emp.Salary;
$('#idTitle').text('Update Employee Detail');
}
$scope.AddNewClick=
function()
{
$scope.ID = "";
$scope.Name
=
"";
$scope.Gender =
"";
$scope.Salary =
"";
$('#idTitle').text('Add Employee Detail');
}
$scope.DeleteEmployee =
function (emp) {
if (confirm("Are you sure you want to delete this Employee?")) {
$http({
method:
"Post",
datatype:
"json",
data: { ID: emp.ID },
url:
"../Home/DeleteEmployee"
}).then(function (response) {
if (response.data.success == true) {
alert(
'Deleted successfully!!!');
$scope.GetAllEmployees();
}
else {
alert(
'Save failed!!!');
}
}, function () {
alert(
"Error
occured!!!"
);
})
}
};
});
Output can be seen below as there are no records initially.
Now click on Add New button to add new record in modal popup.
Now click on Save button to save record and reload the employee records.