Wesley Skeen
Wesley Skeen

Reputation: 8285

Post method sending a null object

I have the following javascript code

function SendMessage(personId) {    

var message = new MessageClass.Message($('#textareaMessage').val(), personId);      
$.ajax({ type: "POST", contentType: 'application/json', url: "/api/PersonApi/SendMessage/" + message }).success(function (data) { });
}

var MessageClass =
{
    Message: function (messageText, personId) {
        this.MessageText = messageText;
        this.PersonId = personId;
    }
};

and the api method is

[System.Web.Http.HttpPost]
public void SendMessage(Message message)
{
    var uow = new Uow();
    var person= uow.People.GetById(message.PersonId);
    person.Messages.Add(message);
}

Every time I post the data to the api, the data is always null.

My Message class is as follows

public class Message
{
    [Key]
    public int Id { get; set; }
    public string MessageText { get; set; }
    [ForeignKey("Person")]
    public int PersonId { get; set; }
    public virtual Person Person { get; set; }
}

Upvotes: 0

Views: 83

Answers (1)

SLaks
SLaks

Reputation: 887453

You need to send the content as the POST payload, not the URL.

Add data: message to the AJAX call.

Upvotes: 1

Related Questions