3

I'm trying to create a model in JavaScript and post it to an ASP.NET MVC controller method via $http.post. When inspecting the JSON that's posted, my Address properties are populated. However, when the ASP.NET MVC controller is hit, all the corresponding Address properties are null.

Angular controller method:

$scope.submitForm = function(isValid) { alert("Form is valid!"); var member = { TempCardNumber: $scope.tcardno, Title: $scope.title, FirstName: $scope.firstName, LastName: $scope.lastName, Address: [ { AddressLine1: $scope.address1, AddressLine2: $scope.address2, AddressLine3: $scope.address3, AddressLine4: $scope.address4, TownCity: $scope.towncity, County: $scope.county, PostCode: $scope.postcode, PhoneNumber: $scope.phone } ], EmailAddress: $scope.email }; $http({ method: 'POST', url: '/Home/Index', data: member }).success(function (data, status, headers, config) { }); } 

Posted JSON:

{"TempCardNumber":"633341677489986320","Title":"Mr","FirstName":"test","LastName":"user","Address":[{"AddressLine1":"8 Eaton Close","AddressLine2":" Hatton","TownCity":" Derby","County":" Derbyshire","PostCode":" DE65 5ED","PhoneNumber":"01234567890"}],"EmailAddress":"[email protected]"} 

ASP.NET MVC controller:

public ActionResult Index([FromBody]MemberModel model) { // do stuff } 

I've tried stringify-ing the JSON, passing it in as just data: member in my Angular post, and using [FromBody] on the ASP.NET MVC method but always, the model.Address properties are null.

Is there anyway I can do this without posting each property to my ASP.NET MVC controller?

Edit: Added MemberModel POCO for clarity

// MemberModel class public string Title {get;set;} public string FirstName {get;set;} public string LastName {get;set;} public Address Address{get;set;} public string TempCardNumber {get;set;} public string Email {get;set;} // Address POCO public string AddressLine1 {get;set;} public string AddressLine2 {get;set;} public string AddressLine3 {get;set;} public string AddressLine4 {get;set;} public string TownCity {get;set;} public string County {get;set;} public string Postcode {get;set;} public string PhoneNumber {get;set;} 
3
  • 1
    BindModel should be around somewhere... If in your POST event on the server side, it should bind based on the Model of the input type, if not then the object is not able to cast properly.. Canyou show us your Class model MemberModel ?
    – Pogrindis
    CommentedMay 26, 2015 at 15:43
  • Do you not need to set the content type somewhere, in order to tell ASP.NET MVC that you are posting JSON? Try adding headers: { "Content-Type": "application/json" } to your $http request.
    – Chris
    CommentedMay 26, 2015 at 15:46
  • MemberModel class added
    – Brett
    CommentedMay 26, 2015 at 18:34

1 Answer 1

-1

Make sure you are using the [HttpPost] attribute on your controller method

4
  • 2
    he has already said his Controller is being hit so i doubt this is the issue. But happy to be wrong.
    – Pogrindis
    CommentedMay 26, 2015 at 16:04
  • Yes, the controller is being hit - I have the [HttpPost] attribute on it
    – Brett
    CommentedMay 26, 2015 at 18:28
  • Have you found a solution for this? I am having the same problem.CommentedJun 16, 2015 at 15:55
  • Try passing the object into JSON.stringify() and use that as your data valueCommentedJun 16, 2015 at 17:50

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.