Zehra Subaş Zehra Subaş - 3 months ago 11
Javascript Question

how to send json data to serializable class

there is a problem when send json data to serializable class. On client side when i check data on browser it is true but on server side the fields of serializable class are null.

Client side (using dojo) : on the debugger line fileds of ticket object are assigned

var Ticket = {
ProductId: productId,
Type: ticketType
};

DataService.xhrPageMethodJson("api/Tickets/MyTickets"
, dojo.toJson(Ticket)
, {
success: function (data) {
debugger
loadSubPage(getRoot("Support/Tickets.aspx?mode=list"));
},
error: function (err) {
LoadingOverlay.hide();
}
});


Web Method: On this method ticket.ProductId, ticket.Type fields has default value or null.

[HttpPost]
[AcceptVerbs("POST")]
[ActionName("MyTickets")]
[Authorize]
public void MyTickets([FromBody]Ticket ticket)
{
DataTable myTickets = db.Tickets.MyTickets(Globals.LogonUser.User.Id, ticket.ProductId, ticket.Type);
WebContext.Session["Tickets"] = myTickets;
}


Ticket class:

[Serializable]
[EntityTable(Name = "Tickets")]
public class Ticket : Entity
{
public override int Id { get; set; }
public string Subject { get; set; }
public string Message { get; set; }
public DateTime SentDate { get; set; }
[JoinField(typeof(User), "Id")]
public int SenderUserId { get; set; }
[JoinField(typeof(Product), "Id")]
public int ProductId { get; set; }
[JoinField(typeof(TicketImportanceLevel), "Id")]
public int ImportanceLevel { get; set; }
public int Analyze { get; set; }
public int SolutionTransactionId { get; set; }
[JoinField(typeof(TicketType), "Id")]
public int Type { get; set; }
public int Status { get; set; }
public int AssignedUserId { get; set; }
public float Score { get; set; }

}

Answer

I have solved this problem with marking properties as data member and class as data contract. Here I am sharing new class structure:

[Serializable]
[DataContract]
[EntityTable(Name = "Tickets")]
public class Ticket : Entity
{
    [DataMember]
    public override int Id { get; set; }
    [DataMember]
    public string Subject { get; set; }
    [DataMember]
    public string Message { get; set; }
    [DataMember]
    public DateTime SentDate { get; set; }
    [JoinField(typeof(User), "Id")]
    [DataMember]
    public int SenderUserId { get; set; }
    [JoinField(typeof(Product), "Id")]
    [DataMember]
    public int ProductId { get; set; }
    [JoinField(typeof(TicketImportanceLevel), "Id")]
    [DataMember]
    public int ImportanceLevel { get; set; }
    [DataMember]
    public int Analyze { get; set; }
    [DataMember]
    public int SolutionTransactionId { get; set; }
    [JoinField(typeof(TicketType), "Id")]
    [DataMember]
    public int Type { get; set; }
    [DataMember]
    public int Status { get; set; }
    [DataMember]
    public int AssignedUserId { get; set; }
    [DataMember]
    public float Score { get; set; }
}