Learn how to add data to the database by using Angularjs.
Today, I learned how to add data to a database using angularjs.
To learn this article, you must start with the previous articles, because there are also demonstrations such as creating data tables.
Create a stored procedure:
SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ongocreate procedure [dbo]. [usp_Goods_Insert] (@ Item NVARCHAR (55), @ Description NVARCHAR (20), @ Qty DECIMAL (10, 2) asif exists (select top 1 FROM [dbo]. [Goods] WHERE [Item] = @ Item) begin raiserror (n' [% s] the Item already exists. ', 16,1, @ Item) returnendelse insert into [dbo]. [Goods] ([Item], [Description], [Qty]) VALUES (@ Item, @ Description, @ Qty) GOSource Code
Add a real-value method, that is, the collaboration between the program and the database:
In the ASP. net mvc controller, add two operations, one for the webpage and the other for the Action:
MVC View:
The above # 2html code:
# 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. string Ify (obj),}). then (function success (response) {if (response. data. Success) {alert ("data added successfully. "); Window. location. href = response. data. redirectUrl;} else {alert (response. data. predictionmessage) ;}, function error (error) {alert (response. error. data );});};});Source Code
Real-time Demonstration: