简体   繁体   中英

How Insert AngularJS JSON Data Into SQL

So I am currently working on an application that uses WebAPI and AngularJS to search for some data from a SQL table and display it on a webpage for the user to select multiple individual rows of data. I am inserting the selected rows into a separate JSON array (availableclients) that I would like to insert into a separate SQL table. What is the best method for me to take my array of JSON data and insert it into a different SQL table. I will attach the code I am currently using to get the data.

Controller.js

var app = angular.module('myApp', []);

app.controller("myController", function ($scope, $http) {

  function getCert(myid) {
    $http.get('api/Cert/Get/', { params: { id : myid } })
      .success(function (data) {
        $scope.selectedclients = data;
      })
  }

  $scope.searchClick = function() {
    getCert($scope.myid);
  }

  $scope.moveItem = function (item, from, to) {
    var idx = from.indexOf(item);
    if (idx != -1) {
      from.splice(idx, 1);
      to.push(item);
    }
  };

  $scope.availableclients = [];
});

HTML

<html data-ng-app="myApp">
  <body data-ng-controller ="myController">    

My_ID: <input type="text" ng-model="my_id" />
    <input type="submit" value="Search" ng-click="searchClick()" />

    <select size="10" multiple ng-model="selected" ng-options="i.unit_id for i in selectedclients" style="width: 400px"></select>
    <div class="btn-group">
      <button title="Remove From List" class="btn btn-default" ng-click="moveItem(available[0], availableclients,selectedclients)"><i class="glyphicon glyphicon-chevron-left"></i></button>
      <button title="Add To List" class="btn btn-default" ng-click="moveItem(selected[0], selectedclients,availableclients)"><i class="glyphicon glyphicon-chevron-right"></i></button>
    </div>
    <select size="10" multiple ng-model="available" ng-options="i.unit_id for i in availableclients" style="width: 400px"></select>
  </body>
</html>

The code I have is working fine I am just at a loss for how to take my availableclients JSON array and insert it into my SQL table. This is probably really easy to do but all my searches are coming up blank on what I am looking for exactly. Any help is appreciated. Thanks!

EDIT 1: On recommendation of a comment I am adding the Controller I used for the Web API get. Again thanks for any advice!

public class CertController : ApiController
{

    CertEntities objapi = new CertEntities();


    [HttpGet]
    public IEnumerable<AngularCoCSelect_Result> Get(int id)
    {

        return objapi.AngularCoCSelect(id).AsEnumerable();
    }

}

It is quite wide question... also you are showing the client code and nothing from the backend which eventually will store the data (suspecting as you put asp.net tag also)

So based on that You can use Entity Framework to store your data into a database. You may find a lot of articles on the internet about implementing this approach.

This solution as guide line

need back-end api that accept array of object (json) for your clients ASP.NET or PHP then

You need to add function in Angular within your controller for submit client data to the server using $http.put or $http.post

$scope.submitClientData = function(){
    $http.post('webapi/clients' , { clients:$scope.availableclients })
}

You may call function from submit button <button ng-click='submitClientData()'>Save</button>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM