I am learning how to send within a POST, two parameters grouped in an object array as payload.
AngularJS:
var parData = { 'panelists': JSON.stringify($scope.arr), 'id': $scope.webId };
$http.post("/api/addPanelists", parData)
.then(function (data, status, headers, config) {
}), function (data, status, headers, config) {
alert("An error occurred during the request");
};
Server Side:
Class Panelists:
public class Panelists
{
public string name { get; set; }
public string email { get; set; }
}
AddPanelists ApiController:
[HttpPost]
public void CreatePanelists(Newtonsoft.Json.Linq.JObject data)
{
List<Panelists_DataImport.Panelist> panelistList = new List<Panelists_DI.Panelist>();
panelistList = data["panelist"].ToObject<List<Panelists_DataImport.Panelist>>();
webID = data["webId"].ToObject<Panelists_DataImport.Webinar>();
}
data contents:
{{ "panelists": "[{\"name\":\"Jack Anderson\",\"email\":\"[email protected]\"},{\"name\":\"Ed Johnson\",\"email\":\"[email protected]\"},{\"name\":\"Dead Poole\",\"email\":\"[email protected]\"},{\"name\":\"Hank Schmidt\",\"email\":\"[email protected]\"},{\"name\":\"Steven Alves\",\"email\":\"[email protected]\"}]", "id": "94395753143"}}
When I get to the line
panelistList = data["panelists"].ToObject<List<Panelists>>();
I am getting this error:
Error converting value "[{"name":"Jack Anderson","email":"[email protected]"},{"name":"Ed Johnson","email":"[email protected]"},{"name":"Dead Poole","email":"[email protected]"},{"name":"Hank Schmidt","email":"[email protected]"},{"name":"Steven Alves","email":"[email protected]"}]" to type 'System.Collections.Generic.List`1[Panelists_DataImport.Panelists]'. Path ''.
How can I correctly retrieve the array from the post request? Am I using the incorrect type?
I am stuck at the moment and any help would be appreciated.
Thank you, Erasmo
UPDATE CODE (not working not yet understanding how to implement from article)
[HttpPost]
public void CreatePanelists(Newtonsoft.Json.Linq.JObject data)
{
string webID = data["id"].ToString();
IList<Panelist> panelistList = new IList<Panelist>;
}
public class Panelist
{
public string name { get; set; }
public string email { get; set; }
}
public class Parameters
{
public IList<Panelist> panelists { get; set; }
public string id { get; set; }
}
UPDATE with screen shots of new code and error window:

