2016-06-10 83 views
-1

我是新來angularJS並試圖找出如何上傳數據到我的服務器,問題是,我的崗位服務器是成功的,它創造新的紀錄,但一切空(NULL)。如何使用angularJS發佈數據到服務器,Web API

感謝您的任何意見

JS:

$scope.addChannel = function() { 
    var channels = []; 
    //var newChannel = { 
    // Name: $scope.Name 
    //}; 
    //clearing error message 
    $scope.errorMessage = ""; 

    $http.post("api/Channel/channels", newChannel) 
     .success(function (newChannel) { 
      //on success 
      $scope.channels.push({ "Name": $scope.Name }); 
      console.log("data added"); 
      // newChannel.push(response.data); 
      newChannel = {}; 
     }, function() { 
      //on failure 
      $scope.errorMessage = "Failed to save data"; 
     }) 
} 

HTML:

<div ng-controller="ChannelController"> 
<div class="col-md-4 col-lg-4 col-sm-4"> 
    <form novalidate name="newUser"ng-submit="addChannel()"> 
     <div class="form-group"> 
      <label for="name">Channel</label> 
      <input class="form-control" type="text" id="Name" name="Name" ng-model="newChannel.Name" /> 
     </div> 
     <div class="form-group"> 
      <input type="submit" value="Add" class="btn btn-success" /> 
     </div> 
    </form> 
    <a ng-href="/Dashboard#/channels">Return to dashboard</a> 
</div> 
<div class="has-error" ng-show="errorMessage"> 
    {{errorMessage}} 
</div> 

通道控制器

 [HttpPost("channels")] 
    public async System.Threading.Tasks.Task Create(Channel channel) 
    { 
     await _channelRepository.CreateChannel(channel); 
    } 

 public async System.Threading.Tasks.Task CreateChannel(Channel channel) 
    { 
     _context.Channel.Add(channel); 
     await _context.SaveChangesAsync(); 
    } 
+2

請張貼的WebAPI方法太 –

+0

什麼是'newChannel'傳遞到$ http.post?你的數據在'$ scope.newChannel.Name'中。 – Paqman

+0

檢查成功回調簽名:https://docs.angularjs.org/api/ng/service/$http – fantarama

回答

0

檢查,如果你的名字正確的對象,您發送到服務器的屬性,屬性的名稱是區分大小寫的。看起來你對JS

var customer = {name: 'foo', lastName: 'bar'}; 

,並在服務器上您嘗試反序列化JSON這個實體

class Customer { 
    public Name {get;set;} //note prop names are capitalized 
    public LastName {get;set;} 
} 
+0

屬性名稱應該是正確的,我使用每個模型屬性都用大寫字母 – user3188501

相關問題