Learn angularjs today to add data to the database.
To learn this article, you have to start with the previous ones, because there are also demonstrations such as creating data tables.
Now to create an added stored procedure:
1 SET ANSI_NULLS ON
2 GO
3
4 SET QUOTED_IDENTIFIER ON
5 GO
6
7
8 CREATE PROCEDURE [dbo]. [Usp_Goods_Insert]
9 (
10 @Item NVARCHAR (55),
11 @Description NVARCHAR (20),
12 @Qty DECIMAL (10,2)
13)
14 AS
15 IF EXISTS (SELECT TOP 1 1 FROM [dbo]. [Goods] WHERE [Item] = @Item)
16 BEGIN
17 RAISERROR (N ‘[% s] item already exists.’, 16, 1, @Item)
18 RETURN
19 END
20 ELSE
21 INSERT INTO [dbo]. [Goods] ([Item], [Description], [Qty]) VALUES (@ Item, @ Description, @ Qty)
22 GO
twenty three
24 Source Code
View Code
Add a real-value method, which is the collaboration between the program and the database:
In the controller of ASP.NET MVC, add 2 operations, one is for web page preparation, and the other is to add Action:
MVC view:
The # 2html code above:
# 3javascript program:
var GoodsApp = angular.module (‘GoodsApp’, []);
GoodsApp.controller (‘GoodsAdditionController’, function ($ scope, $ http) {
$ scope.GoodsAddition = function () {
var obj = {};
obj.Item = $ scope.Item;
obj.Description = $ scope.Description;
obj.Qty = $ scope.Qty;
$ http ({
method: ‘POST’,
url: ‘/ Goods / Insert’,
dataType: ‘json’,
headers: {
‘Content-Type’: ‘application / json; charset = utf-8’
},
data: JSON.stringify (obj),
}). then (
function success (response) {
if (response.data.Success) {
alert ("Data added successfully.");
window.location.href = response.data.RedirectUrl;
}
else {
alert (response.data.ExceptionMessage);
}
},
function error (error) {
alert (response.error.data);
});
};
});
Source Code
Real-time demo:
[Transfer from: http://www.cnblogs.com/insus/p/6858290.html]
Learn Angularjs to add data to the database