0

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:

code

error

1 Answer 1

1

You are doing something wrong here.

Valid JSON will be

{
    "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"
}

and class should be like this.

public class Panelist
{
    public string name { get; set; }
    public string email { get; set; }
}

public class Example
{
    public IList<Panelist> panelists { get; set; }
    public string id { get; set; }
}

and you should access it like this.

data.panelists not like this data["panelists"]

Update:

[HttpPost]
public void CreatePanelists(string data)
{

    Parameters parameters = JsonConvert.DeserializeObject<Parameters>(data);

    IList<Panelist> panelistList = parameters.panelists;
}
Sign up to request clarification or add additional context in comments.

9 Comments

Hello @vivek nuna - Thank you for your answer. I hope I'm not asking much, would it be possible to show me how to implement your suggestion within the code I provided? I think it would be easier for me to learn that way. Thank you again.
@erasmocarlos please see this example, its very easy newtonsoft.com/json/help/html/SerializingJSON.htm
If not like this data["panelists"], why webID = data["webId"].ToObject<Panelists_DataImport.Webinar>(); works well?
No I am trying but I am not getting it. I'm sorry
uppdate the question with your latest code and error details and update me then
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.