Learn how to add data to the database by using Angularjs.

Source: Internet
Author: User

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:


 

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.