如何将AngularJS JSON数据插入到SQL中?

时间:2021-07-20 16:53:02

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.

因此,我目前正在开发一个应用程序,它使用WebAPI和AngularJS来从SQL表中搜索一些数据,并在一个页面上显示它,以便用户选择多个单独的数据行。我将所选的行插入到一个单独的JSON数组(availableclients)中,我希望将其插入到一个单独的SQL表中。对我来说,最好的方法是将我的JSON数据数组插入到一个不同的SQL表中。我将附加我当前使用的代码来获取数据。

Controller.js

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

<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!

我的代码工作得很好,我只是想知道如何使用JSON数组并将其插入到SQL表中。这很可能很容易做到,但是我的所有搜索都在我正在寻找的东西上出现了空白。任何帮助都是感激。谢谢!

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

编辑1:关于评论的建议,我添加了用于Web API的控制器。再次感谢您的建议!

public class CertController : ApiController
{

    CertEntities objapi = new CertEntities();


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

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

}

2 个解决方案

#1


0  

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)

这是一个很宽泛的问题……同时,您还显示了客户端代码,并且没有任何来自后端的代码,最终将存储这些数据(怀疑您将asp.net标记也放入其中)

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.

基于此,您可以使用实体框架将数据存储到数据库中。你可以在网上找到很多关于实施这种方法的文章。

#2


0  

This solution as guide line

这个解决方案作为指导方针。

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

需要后端api来接受客户端的对象数组(json)。净或PHP那么

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

您需要在控制器中添加函数,以使用$http向服务器提交客户端数据。把美元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>

您可以从submit按钮中调用函数

#1


0  

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)

这是一个很宽泛的问题……同时,您还显示了客户端代码,并且没有任何来自后端的代码,最终将存储这些数据(怀疑您将asp.net标记也放入其中)

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.

基于此,您可以使用实体框架将数据存储到数据库中。你可以在网上找到很多关于实施这种方法的文章。

#2


0  

This solution as guide line

这个解决方案作为指导方针。

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

需要后端api来接受客户端的对象数组(json)。净或PHP那么

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

您需要在控制器中添加函数,以使用$http向服务器提交客户端数据。把美元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>

您可以从submit按钮中调用函数