我有以下 javascript 代码
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;
}
};
api方法是
[System.Web.Http.HttpPost]
public void SendMessage(Message message)
{
var uow = new Uow();
var person= uow.People.GetById(message.PersonId);
person.Messages.Add(message);
}
每次我将数据发布到 api 时,数据始终为空。
我的Message
课如下
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; }
}