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? Commented May 1, 2015 at 21:29
  • NullPointerException when i want acces to "Invites" Array object Commented May 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.

Sign up to request clarification or add additional context in comments.

1 Comment

"you are one guly motherf*er " Thanks this helped me.
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 Comments

@Amadou why CalendarEvent properties are private
it's private but a have getters et setters methods in my class.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.