2016-04-20 99 views
1

我被卡在我的SQL Server数据库中的“$ scope.arr”中推送保存多行。我有我的代码如下,它工作正常,但当我点击“保存到数据库”按钮后按“添加人员”按钮添加/推动一些条目,它传递一个具有空值的行到SQL Server数据库。请指导我在哪里我犯的错误:推动多个条目,并通过AngularJS将它们保存在数据库中

我也听说过使用angular.forEach循环,但我也很困惑。

我有我的模型类 “TestModel.cs” 在这里:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 

namespace TestProj.Models 
{ 
    public class TestModel 
    { 
     public int ID { get; set; } 
     public string FirstName { get; set; } 
     public string LastName { get; set; } 
    } 
} 

我的MVC控制器命名的TestController的Add方法在这里:

[HttpPost] 
public string AddPerson(TestModel test) 
      using (TestContext db = new TestContext()) 
      { 
       if (test != null) 
       { 
        db.TestModels.Add(test); 
        db.SaveChanges(); 
        return "Successful"; 
       } 
       else 
       { 
        return "Failed"; 
       } 
      } 
     } 

我AngularJS脚本的位置:

var app = angular.module("TestApp", []); 

app.controller("TestCtrl", function ($scope, TestService) { 

    $scope.arr = []; 

    $scope.addPerson = function() { 
     var myobj = { 
      FirstName: $scope.firstname, 
      LastName: $scope.lastname 
     } 
     $scope.arr.push(myobj); 
    }; 

    $scope.savePerson = function() { 
     var TestData = TestService.AddPer($scope.arr); 
     TestData.then(function (msg) { 
      alert(msg.data); 
     }, function() { 
      alert('Error In Adding Person'); 
     }); 
    }; 

}); 

app.service("TestService", function ($http) { 

    this.AddPer = function (person) { 
     var response = $http({ 
      method: "post", 
      url: "/Test/AddPerson", 
      data: JSON.stringify(person), 
      dataType: "json", 
     }); 
     console.log(response); 
     return response; 
    } 
}); 

和我的Index.cshtml文件在这里:

<div ng-controller="TestCtrl"> 
    <form> 
     FirstName: <input class="form-control" ng-model="firstname" /><br /> 
     LastName: <input class="form-control" ng-model="lastname" /><br /> 
     <button class="btn btn-success" ng-click="addPerson()">Add Person</button> 
     <button class="btn btn-success" ng-click="savePerson()">Save To DB</button> 

     <table class="table table-bordered"> 
      <tr> 
       <th>S. No</th> 
       <th>First Name</th> 
       <th>Last Name</th> 
      </tr> 
      <tr ng-repeat="a in arr"> 
       <td>{{$index+1}}</td> 
       <td>{{a.FirstName}}</td> 
       <td>{{a.LastName}}</td> 
      </tr> 
     </table> 
    </form> 
</div> 

<script src="~/Scripts/MyAngular/Module.js"></script> 

您的帮助将不胜感激。谢谢!

回答

1

然后服务器端的行动应该期望收集TestModel,你可以在那里我们List。如果您在参数之前[FromBody],则在将其发布到服务器之前不需要stringify数据。

代码

[HttpPost] 
public string AddPerson([FromBody] List<TestModel> test) 
    using(TestContext db = new TestContext()) { 
    test.forEach(t=> { 
     if (t != null) { 
      db.TestModels.Add(t); 
      return "Successful"; 
     } else { 
      return "Failed"; 
     } 
    }); 
    db.SaveChanges(); //save context at the end, when no error occurs 
    } 
} 
+0

这种技术没有工作,但这种非常紧密的方法工作在我这样做的地方: [HttpPost] public void AddPerson(List test) { using(TestContext db = new的TestContext()){ 的foreach (测试VAR吨) { 如果(T = NULL){ db.TestModels.Add(吨)!; Console.WriteLine(“Success”); } } db.SaveChanges(); //保存上下文,当没有错误发生时 } } –

+0

你只改变了.forEach lambda to simple forEach ..我看不到其他的变化..我想我的答案是正确的..尽管你写了你的:D –

+0

你的回答很完美@Pankaj Parkar。但我的Visual Studio没有加载程序集我不知道为什么:/感谢无论如何帮助:) –

1

的问题是在我的服务器端代码,即,我的C#控制器,此方法处理:

[HttpPost] 
     public void AddPerson(List<TestModel> test) 
     { 
      using (TestContext db = new TestContext()) 
      { 
       foreach(var t in test) 
       { 
        if (t != null) 
        { 
         db.TestModels.Add(t); 
         Console.WriteLine("Success"); 
        } 
       } 
       db.SaveChanges(); //save context at the end, when no error occurs 
      } 
     } 
相关问题