0

I want to post an JavaScript object from AngularJS to C# Api The simple type is posted but not object et Array of object

var event = { dateDebut: dateDebut, heureDebut: heureDebut, heureFin: heureFin, invites: invites,// Array Of Object titreRdv: titreRdv, userEmail: EmailGoogle } $http.post(CreateRdvPath, event) .success(function (data) { // Success code }) .error(function (data) { // Error code }); 

My C# Controller function

[HttpPost] public string createRdv(CalendarEvent MyEvent) { } 

CalendarEvent

public class CalendarEvent { private string dateDebut, heureDebut, heureFin, titreRdv; private string userEmail; private List<Utilisateur> invites; } 
2
  • what error you are getting in console? or on server side?CommentedMay 1, 2015 at 21:29
  • NullPointerException when i want acces to "Invites" Array object
    – Amadou
    CommentedMay 1, 2015 at 22:08

2 Answers 2

1

You must supply a default constructor for any class that contains other objects. In your case, add this constructor that initializes the object.

public class CalendarEvent { public CalendarEvent() { invites = new List<Utilsateur>(); } private string dateDebut, heureDebut, heureFin, titreRdv; private string userEmail; private List<Utilisateur> invites; } 

This should then allow the framework to correctly parse the object.

1
  • 1
    "you are one guly motherf*er " Thanks this helped me.
    – IronHide
    CommentedAug 9, 2016 at 12:43
0

Your $http.post should pass data as {MyEvent: event} with server side action parameter name

Code

$http.post(CreateRdvPath, {MyEvent: event}) .success(function(data) { // Success code }) .error(function(data) { // Error code }); 

OR

$http.post(CreateRdvPath, JSON.stringify({MyEvent: event})) .success(function(data) { // Success code }) .error(function(data) { // Error code }); 
2
  • @Amadou why CalendarEvent properties are privateCommentedMay 1, 2015 at 22:38
  • it's private but a have getters et setters methods in my class.
    – Amadou
    CommentedMay 1, 2015 at 22:52

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.