0

I have the following post request using $http

var registration = {
   userId: 23,
   groupings: [ 
           { Id: 1, Name: 'Test Group 1', Description: 'Yo' }, 
           { Id: 4, Name: 'Test Group 4', Description: 'Er' } 
]
}
$http({
            method: 'POST',
            url: url,
            headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
            data: $.param({
                code: eventCode,
                UserId: registration.userId
                Groupings: registration.groupings
            })
        })

And then on my action

    [HttpPost]
            public ActionResult New(string code, RegistrationVM model)
            {
    :
    :
    :
    }


    public class RegistrationVM  
        {
             public int UserId {get;set;}
             public IEnumerable<GroupingVM> Groupings { get; set; }
    }

public class GroupingVM{
   public int Id {get;set;}
   public string Name {get;set;}
   public string Description {get;set;}

   public int AnotherPropertyId {get;set;}
   public ANewClass ANewClass {get;set;}
}

Whenever the post happens, I would have the model properties reflect what I posted except the IEnumerable (Groupings) property. Let's say I post with 2 objects on the Groupings property, when I go to the Action, the Groupings property will have a count of 2 but every instance would have either NULL or 0 values on each object. I'm trying to figure out how I'm messing the post.

10
  • 1
    Shouldn't the params be: $.param({ code: eventCode, model: registration }) Commented Sep 22, 2016 at 13:11
  • Yeah I tried that, it didn't work.With that, everything is empty now except code. Commented Sep 22, 2016 at 13:21
  • Try and add the string code to the RegistrationVM class and use one parameter in the controller method. Also add the code to the registration object in the client and pass $.param({ model: registration }) Commented Sep 22, 2016 at 13:37
  • Tried it, moved the properties to the VM, had the same effect, the groupings would have the right count but it's still empty properties. Should I be filling up all properties with values? Commented Sep 22, 2016 at 13:52
  • 1
    data: JSON.stringify(registration).. Another option is to use a DTO just for this method that only includes the properties you need Commented Sep 22, 2016 at 14:01

1 Answer 1

1

First, make the controller take one object as a parameter. Add the "string code" to the RegistrationVM and remove the string code as a parameter.

Then do the same when you create the client object.

Secondly, instead of using the content type "x-www-form-urlencoded" use the "application/json" which is better suited to pass objects.

Before posting the data you need to stringify the obj with data: JSON.stringify(registration)

For more details, read the comments on the question.

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

Comments

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.