2016-05-08 11 views
1

現在、会計プロジェクトに取り組んでいますが、私は愚かな問題が発生しています。 テキストボックスに値を入力し、[人を追加]ボタンをクリックすると値が追加されますが、[データベースに保存]ボタンをクリックすると、複数の追加された値がデータベースにプッシュされます。データベースに複数の値を挿入することはできますが、複数のエントリをSQLサーバーから再度呼び出すことはできません。文字列の値がAngularJSの配列で取得されていない

私はSQLサーバーのテーブルに複数の同じファーストネームを持っています。同じ名前のすべての値を取得して、$ scope.arrに値を設定したいと思います。角度コードで定義しました。

$ scope.GetPerson()メソッドで整数値を渡すと、目的の結果が得られ、配列($ scope.arr)に同じCustomerCodeを持つすべての値が設定されますが、文字列を渡すと値、つまりGetPersonメソッドのFirstNameは、配列($ scope.arr)にデータベースに配置された名前と同じ名前が設定されません。 私は実際にmistake.`をやっているところ私は

の専門家がこの問題に見て、私を案内したい

マイモデル:

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

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

マイ角度JSスクリプト:

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

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

    $scope.arr = []; 


    GetAllPers(); 
    function GetAllPers() { 
     var getPersData = TestService.getPersons(); 
     getPersData.then(function (Persons) { 
      $scope.persons = Persons.data; 
     }, function() { 
      alert('Error in getting Person records'); 
     }); 
    } 

    $scope.addPerson = function() { 
     var obj = { 
      CustomerCode: $scope.customercode, 
      FirstName: $scope.firstname, 
      LastName: $scope.lastname, 
      Debit: $scope.debit, 
      Credit: $scope.credit, 
     }; 
     $scope.arr.push(obj); 
    }; 

    $scope.savePerson = function() { 
     var TestData = TestService.AddPer($scope.arr); 
     TestData.then(function (msg) { 
      $scope.customercode = ""; 
      $scope.firstname = ""; 
      $scope.lastname = ""; 
      $scope.debit = ""; 
      $scope.credit = ""; 
      GetAllPers(); 
      alert(msg.data); 
      for (var i = 0; i < $scope.arr.length; i++) { 
       $scope.arr.pop(); 
      } 
     }, function() { 
      alert('Error In Adding Person'); 
     }); 
    }; 

    $scope.GetPerson = function (test) { 
     var getTestData = TestService.getPers(test.FirstName); 
     getTestData.then(function (_test) { 
      $scope.arr = _test.data; 
     }, function() { 
      alert('Error in getting person records'); 
     }); 
    } 

}); 

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

    this.getPersons = function() { 
     return $http.get("/Test/GetAllPers"); 
    } 

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

    this.getPers = function (persname) { 
     var response = $http({ 
      method: "post", 
      url: "/Test/GetPersonByFName", 
      params: { 
       firstname: JSON.stringify(persname) 
      } 
     }); 
     return response; 
    } 
}); 

私のC#コントローラ:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.Web.Mvc; 
using TestProj.Models; 

namespace TestProj.Controllers 
{ 
    public class TestController : Controller 
    { 
     TestContext dc = new TestContext(); 
     // GET: Test 
     public ActionResult Index() 
     { 
      return View(); 
     } 
     public JsonResult GetAllPers() 
     { 
      var personList = dc.TestModels.ToList(); 
      return Json(personList, JsonRequestBehavior.AllowGet); 
     } 
     public JsonResult GetPersonByFName(string firstname) 
     { 
      var q = (from ab in dc.TestModels.Where(b => b.FirstName == firstname) select ab); 
      return Json(q, JsonRequestBehavior.AllowGet); 
     } 

     [HttpPost] 
     public string AddPerson(List<TestModel> test) 
     { 
      bool val = false; 
      foreach (var t in test) 
      { 
       if (t != null) 
       { 
        dc.TestModels.Add(t); 
        val = true; 
       } 
       else 
       { 
        val = false; 
       } 
      } 
      dc.SaveChanges(); //save context at the end, when no error occurs 
      if (val == true) 
      { 
       return "Success"; 
      } 
      else 
      { 
       return "Failed"; 
      } 
     } 
    } 
} 

自分のHTML:

@{ 
    ViewBag.Title = "Index"; 
} 

<h2>Index</h2> 

<div ng-controller="TestCtrl"> 
    <form> 
     <button class="btn" data-toggle="modal" data-target="#myModal">Show records</button><br /> 
     Customer Code: <input class="form-control" ng-model="customercode" /><br /> 
     FirstName: <input class="form-control" ng-model="firstname" /><br /> 
     LastName: <input class="form-control" ng-model="lastname" /><br /> 
     Debit: <input class="form-control" ng-model="debit" /><br /> 
     Credit: <input class="form-control" ng-model="credit" /><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 for showing pushed values*@ 
     <table class="table table-bordered"> 
      <tr> 
       <th>First Name</th> 
       <th>Last Name</th> 
       <th>Debit</th> 
       <th>Credit</th> 
      </tr> 
      <tr ng-repeat="a in arr"> 
       <td>{{a.CustomerCode}}</td> 
       <td>{{a.FirstName}}</td> 
       <td>{{a.LastName}}</td> 
       <td>{{a.Debit}}</td> 
       <td>{{a.Credit}}</td> 
      </tr> 
     </table> 
     @*Modal popup*@ 
     <div class="modal fade" role="dialog" id="myModal"> 
      <div class="modal-dialog"> 
       <div class="modal-content"> 
        <div class="modal-header"> 
         <button type="button" class="close" data-dismiss="modal">&times;</button> 
         <h4>Persons</h4> 
        </div> 
        <div class="modal-body"> 
         <label class="control-label col-md-2" style="width: auto">Search:</label> 
         <div class="col-md-8"> 
          <input class="form-control" ng-model="searchfor" /> 
         </div><br /> 
         <table class="table table-hover"> 
          <tr> 
           <td><b>First Name</b></td> 
           <td><b>Last Name</b></td> 
           <td><b>Debit</b></td> 
           <td><b>Credit</b></td> 
           <td><b>Select</b></td> 
          </tr> 
          <tr ng-repeat="b in persons | filter:searchfor "> 
           <td>{{b.CustomerCode}}</td> 
           <td>{{b.FirstName}}</td> 
           <td>{{b.LastName}}</td> 
           <td>{{b.Debit}}</td> 
           <td>{{b.Credit}}</td> 
           <td> 
            <button type="button" class="btn btn-success" ng-click="GetPerson(b)" data-dismiss="modal">Select</button> 
           </td> 
          </tr> 
         </table> 
        </div> 
        <div class="modal-footer"> 
         <button type="button" class="btn btn-success" data-toggle="modal" data-target="#myModal">Okay</button> 
        </div> 
       </div> 
      </div> 
     </div><br /> 
    </form> 
</div> 

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

あなたは正しい値(firatName)を通過しているかどうかを確認するためにあなたのdevのツールのネットワークタブを点検し – Shyju

+0

私は最後に、LINQクエリをVS2015でのすべての行をデバッグ( var q)は、フォーマット "\" Rehan \ ""のファーストネームを返しました。私はそれを "Rehan"に変更したとき、うまくいった。私はなぜこれが起こっているのか知りません。 –

答えて

1

問題はgetPers()です。 JSON.stringfy()を追加すると、文字列に"が追加されます。

それは次のように使用する必要があります。

this.getPers = function (persname) { 
    var response = $http({ 
     method: "post", 
     url: "/Test/GetPersonByFName", 
     params: { 
      firstname: persname 
     } 
    }); 
    return response; 
} 
+0

ありがとうございました!これは問題を解決しました。サーバーサイドのコードで何をしたのですか?つまり、私のGetPersonByFNameメソッドはこれでした: string fn = firstname; f =置き換え( "\" "、" "); var q = db.VM.Where(b => b.FirstName == f); これも私の問題を解決しました:)とにかくあなたのコードは走っている! –

関連する問題