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;}
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 modelMemberModel
?headers: { "Content-Type": "application/json" }
to your$http
request.