Learning Angularjs Adding data to a database

Source: Internet
Author: User

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


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.