2016-06-10 81 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

相关问题