Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

is there any example to post multiple objects to a controller. How the data for the ajax post have to look like ?

[HttpPost]
public string Register(UserLogin userLogin, Contact contact)
{
}

UserLogin

public class UserLogin 
{
   public string Username { get; set; }
   public string Password { get; set; }
}

Contact

public class Contact
{
   public string Firstname { get; set; }
   public string Lastname { get; set; }
}

AJAX ?

$.ajax({
   type: "POST",
   url: "SomeUrl"
   dataType: "json",
   contentType: "application/json; charset=utf-8",
   data: ? });
share|improve this question
add comment

3 Answers

up vote 4 down vote accepted

Try this

$.ajax({
   type: "POST",
   url: "SomeUrl"
   dataType: "json",
   contentType: "application/json; charset=utf-8",
   data: {
       'userLogin' : {
           'Username' : 'Username',
           'Password' : 'Password'
       },
       'contact' : {
           'Firstname' : 'Firstname',
           'Lastname' : 'Lastname'
       }
   }
});
share|improve this answer
add comment

Just need to change javascript. Pass your object like this after creating

var loginObject = {
  Username: uname,//get it using jQuery $('#Username').val()
  Password : pswrd    //same way
};
var contact = {
  Firstname = "",
  Lastname = "",
};

And in ajax call,

data: {userLogin: loginObject, contact: contactObject}
share|improve this answer
add comment

You can follow the below snippet

var userLogin = { UserName : "", Password : "" };

var contact = { FirstName : "", LastName : "" };

Then you can assin the data to the ajax call like data : {userLogin : userLogin, contact : contact}

share|improve this answer
add comment

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.