1

Using the below code, I am able to pass this content to my webapi which receives it like

public void Post(Business.Entities.api.newsalert alert)
{
    //do stuff here
}

category = $('#dt_category').val();
title = $('#inputTitle').val();
url = $('#inputURL').val();
comments = $('#inputComments').val();
subject = "News Alert / Alerte Nouvelles: " + title;

var dataJSON = {
    userid: username,
    to: "[email protected]",
    url: url,
    subject: subject,
    title: title,
    source: "My company",
    comments: comments,
    category: category
};

$.ajax({
    type: 'POST',
    url: "http://mywebserver/api/NewsAlerts",
    data: JSON.stringify(dataJSON),
    contentType: 'application/json; charset=utf-8',
    success: function (data) {
        q.resolve();
    }
});

However, my boss has asked me to make the Categories select use multiple..

Therefore I did this

    category = $('#dt_category').val(); //apples, oranges, peaches
    title = $('#inputTitle').val();
    url = $('#inputURL').val();
    comments = $('#inputComments').val();
    subject = "News Alert / Alerte Nouvelles: " + title;

    var categories = [];
    $(category).each(function (index) {
        categories.push({ 'category': category[index] });
    });

    var dataJSON = {
        userid: username,
        to: "[email protected]",
        url: url,
        subject: subject,
        title: title,
        source: "My company",
        comments: comments,
        category: categories 
    };

    $.ajax({
        type: 'POST',
        url: "http://mywebserver/api/NewsAlerts",
        data: JSON.stringify(dataJSON),
        contentType: 'application/json; charset=utf-8',
        success: function (data) {
            q.resolve();
        }
    });

But when my webapi receives the ajax Put, it is always null. I'm assuming I need to somehow make the Categories into some kind of a child node but i'm not sure.

3
  • 1
    is your model Business.Entities.api.newsalert expecting a list ? meaning Business.Entities.api.newsalert.Category is typeof List<int> Commented Jun 16, 2017 at 19:56
  • 1
    you can try by changing category datatype as List<string> Commented Jun 16, 2017 at 19:57
  • Duh, hadn't considered my business object might be the problem, just assumed it would be the json serialistion. Will give it a look, thx. Commented Jun 16, 2017 at 19:59

1 Answer 1

1

Your api request object should contain a property that is some collection type of a class with the property "category".

public class newsalert
{
     //Other Properties         

     [JsonProperty(PropertyName = "category")]
     IList<Category> Categories {get;set;}
}

public class Category
{
     public string category {get;set;}
}
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.