Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.

Join them; it only takes a minute:

Sign up
Join the Stack Overflow community to:
  1. Ask programming questions
  2. Answer and help your peers
  3. Get recognized for your expertise

I am trying to post form data to an ASP.NET Web API with angular. So I have:

<form name="form" data-ng-controller="SubscriberController">
  <input data-ng-model="subscriber.name" name="name" type="text"/>
  <input data-ng-model="subscriber.email" name="email" type="text"/>
  <input data-ng-click="create(subscriber)" type="submit" value="Send" />
</form>

application.service('SubscriberService', function ($http) {
  return {
    Create: function (subscriber) {
      console.log(subscriber);
      return $http.post('api/subscribers/create', { 'subscriber': subscriber });
    }
  }
});

application.controller('SubscriberController', function SubscriberController($scope, SubscriberService) {
  $scope.create = function (subscriber) {
    SubscriberService.Create(subscriber)
      .success(function (data, status, headers, config) { })
      .error(function (data, status, headers, config) { });
  };
});

Finally on the ASP.NET API I have the following:

public class SubscriberController : ApiController {
  [Route("api/subscribers/create"), HttpPost]
  public void Create([FromBody]SubscriberCreateModel subscriber) {
     // Create subscriber
  } // Create
}

public class SubscriberCreateModel {
  public Int32 Country { get; set; }
  public String Email { get; set; }
  public String Name { get; set; }        
}

On my angular service I am logging the subscriber and I get:

Object { name="john", email="[email protected]" }

The problem is that on the controller both subscriber properties, email and name, are null ...

Any idea what I am missing?

share|improve this question
    
declare $scope.subscriber = {}, inside your controller.. – Ved Jan 19 '15 at 12:21

You need to encode the data and send the data as url encoded, check my article about how to use web api 2 with angularjs here http://www.codeproject.com/Articles/742532/Using-Web-API-Individual-User-Account-plus-CORS-En

Hope this helps

share|improve this answer
    
I added the following on my angular config: $http.defaults.headers.post["Content-Type"] = "application/x-www-form-urlencoded"; ... this seems to solve the problem ... – Miguel Jan 19 '15 at 13:58
    
So your problem is solved now? – Omar.Alani Jan 19 '15 at 20:09

You have to create object for SubscriberController as subscriber in the html.

<form name="form" data-ng-controller="SubscriberController as subscriber">
  <input data-ng-model="subscriber.name" name="name" type="text"/>
  <input data-ng-model="subscriber.email" name="email" type="text"/>
  <input data-ng-click="create(subscriber)" type="submit" value="Send" />
</form>
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.