Learn Angularjs today to add data to a database.
To learn this, you have to start with a few of the previous ones, as there are demos that create data tables.
Now create a stored procedure to add:
SETAnsi_nulls onGOSETQuoted_identifier onGOCREATE PROCEDURE [dbo].[Usp_goods_insert]( @Item NVARCHAR( -), @Description NVARCHAR( -), @Qty DECIMAL(Ten,2)) asIF EXISTS(SELECT TOP 1 1 from [dbo].[Goods] WHERE [Item] = @Item)BEGIN RAISERROR(N'[%s] item already exists.', -,1,@Item) RETURNENDELSE INSERT into [dbo].[Goods]([Item],[Description],[Qty])VALUES(@Item,@Description,@Qty)GO
Source Code
Add a real-valued method, which is the collaboration between the program and the database:
In the Controller for ASP. NET MVC, add 2 actions, one for the Web page and one for the action:
MVC view:
Where the above #2html code:
#3javascript程序:
varGoodsapp = Angular.module (' Goodsapp '), []); Goodsapp.controller (' Goodsadditioncontroller ',function($scope, $http) {$scope. Goodsaddition=function () { varobj = {}; 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 (functionSuccess (response) {if(response.data.Success) {alert ("Data added successfully. "); Window.location.href=Response.data.RedirectUrl; } Else{alert (response.data.ExceptionMessage); } }, functionError (Error) {alert (response.error.data); }); }; });
Source Code
Live Demo:
Learning Angularjs Adding data to a database