2

Salaamun Alekum

I am getting null in controller action via an AJAX request:

var ProjectPermission = [{ 
    "CreatedBy": "Akshay" 
},{ 
    "CreatedBy": "Kumar" 
},{ 
    "CreatedBy": "ETC" 
}]

$.ajax({
    url: '/api/Projects/AssignProjectPermissions', 
    type: 'POST', 
    contentType: 'application/json', 
    dataType: 'json', 
    data: JSON.stringify({ 
        ProjectPermission: ProjectPermission 
    }), 
    success: function (data) { 
        alert(data); 
    },
    // processData: false //Doesn't help
});

My C# controller:

[System.Web.Http.HttpPost, System.Web.Http.HttpGet]
public string AssignProjectPermissions(ProjectPermission[] ProjectPermission)
{

I am getting null in ProjectPermission. I already tried other answers but none of them worked for me. These were the posts I checked:

Thank You

Community
  • 1
  • 1
Ali Jamal
  • 844
  • 11
  • 19

1 Answers1

4

You should not be using GET and POST on the same method first of all, just use POST in this case. That aside you do not need the property name. You are putting your array inside of an object. Your method is expecting an array.

var ProjectPermission = [{ CreatedBy: "Akshay" },
               { CreatedBy: "Kumar" },
               { CreatedBy: "ETC" }]
    $.ajax({
        url: '/api/Projects/AssignProjectPermissions'
    , type: 'POST'
    , contentType: 'application/json'
    , dataType: 'json'
    , data: JSON.stringify(ProjectPermission) //<------------issue here
    , success: function (data) 
         { alert(data); }
        //, processData: false 
    });
Stephen Brickner
  • 2,584
  • 1
  • 11
  • 19