0

I have the following mvc viewmodel

public class PersonVM {
    public int Id {get;set;}
    public string Name {get;set;}
    public IEnumerable<DeptVM> Departments {get;set;}
}

public class DeptVM{
    public int Id {get;set;}
    public string Name {get;set;}
}

this is my post function

$scope.selectedDepartments = [{ Id: 1, Name: 'TestDept'}, { Id:2, Name:'Twoooo'}];

$http({
      method: 'POST',
      url: url,
      headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
      data: $.param({
          Id: $scope.personid,
          Departments: $scope.selectedDepartments
      })
}).success(function(data){
});

public ActionResult Edit(int evtId, PersonVM model){

}

On the post, of my ActionResult, the model would have the Id and any other model properties, but the Departments would have 2 counts but all the properties are empty or default.

gdubs
  • 2,528
  • 9
  • 46
  • 89
  • Why are you using $.param? Just pass the object literal. And content type should be 'application/json' – pixelbits Dec 27 '14 at 03:56
  • i have a ValidateAntiForgeryTokenAttribute that is required on post, and it only gets through using $.param – gdubs Dec 27 '14 at 04:02
  • I normally pass the antiforgerytoken in a header like this: http://blogs.msdn.com/b/passing_anti-forgery_token_to_mvc_actions_using_ajax_post/archive/2014/06/30/passing-anti-forgery-token-to-mvc-actions-using-ajax-post.aspx – Wayne Ellery Dec 27 '14 at 05:05
  • did this, and the ienumerable is still empty http://stackoverflow.com/questions/2906754/how-can-i-supply-an-antiforgerytoken-when-posting-json-data-using-ajax – gdubs Dec 27 '14 at 05:40
  • can you attach fiddle please. – Pankaj Parkar Dec 27 '14 at 05:43

0 Answers0