Skip to content Skip to sidebar Skip to footer

Pass Widget Data Using Json And Saving To Database

I have just come back to this requirement again as I was pulled away from it to do more important requirements at the time. I have asked a similar question here and the answer has

Solution 1:

The problem seems to be that the data member names specified in your c# data contract do not match the JSON property names in the JSON you are generating. Your JavaScript code generates JSON that looks like

{"items":[{"id":"1","collapsed":"False","order":"1","column":"1"}]}

But these property names are not the property names in your c# classes, and you have not overridden those names. Try something like the following instead:

[DataContract]
publicclassSaveWidgetsDAL
{
    [DataMember(Name="items")]
    public List<Widgets> wdata { get; set; }

    publicSaveWidgetsDAL() { }

    [DataContract]
    publicclassWidgets
    {
        // I was able to figure out which JSON properties to which to map these properties.
        [DataMember(Name = "column")]
        publicstring ColumnId { get; set; }

        [DataMember(Name = "collapsed")]
        publicstring Collapsed { get; set; }


        // However it is unclear how to map these to your JSON.  
        [DataMember(Name = "sortno")]
        publicstring SortNo { get; set; }

        [DataMember(Name = "title")]
        publicstring Title { get; set; }

        [DataMember(Name = "userid")]
        publicstring UserId { get; set; }

        [DataMember(Name = "widgetid")]
        publicstring WidgetId { get; set; }
    }
}

I was able to deduce the correct c# data member names for collapsed: collapsed and column: columnId, however I could not figure out how to map the rest since they don't seem to match up 1-1. You will need to further fix the data member names to make them match exactly.

Update2

In your updated question, you omitted the [DataContract] attribute on the nested Widgets class:

// [DataContract] missingpublicclassWidgets
    {

You need to make sure both the outer and nested classes have this attribute.

Update

This is the part of your code that creates your JSON:

var collapsed = 0;
            if ($(this).find('.dragbox-content').css('display') == "none")
                collapsed = 1;
            //Create Item object for current panel  var item = {
                id: $(this).attr('id'),
                collapsed: collapsed,
                order: i,
                column: columnId
            };
            //Push item object into items array  
            items.push(item);

        var json = JSON.stringify(sortorder);

Thus each objectitem in your items array contains just these four named properties:

  • id
  • collapsed
  • order
  • column

The property names you use in your var item = { id: value1, collapsed: value2, ...}; statement are the names that JSON.stringify() writes into the json string. The data member names specified in your c# code must match these names exactly in order to deserialize them with DataContractJsonSerializer. Thus the following c# classes will deserialize those four named properties:

[DataContract]
publicclassSaveWidgetsDAL
{
    [DataMember(Name = "items")]
    public List<Widgets> wdata { get; set; }

    publicSaveWidgetsDAL() { }

    [DataContract]
    publicclassWidgets
    {
        // I was able to figure out which JSON properties to which to map these properties.
        [DataMember(Name = "id")]
        publicstring Id { get; set; }

        [DataMember(Name = "collapsed")]
        publicstring Collapsed { get; set; }

        [DataMember(Name = "order")]
        publicstring Order { get; set; }

        [DataMember(Name = "column")]
        publicstring ColumnId { get; set; }
    }
}

If you need to transfer additional properties, you need to add them in your var item statement, then add properties with the identical data member name in your Widgets class. To confirm you have correctly matched the names, you can either debug your ProcessRequest() method with Visual Studio and manually examine your json string, or debug log your json string via:

    System.Diagnostics.Debug.WriteLine(json);

That will allow you to see the JSON and ensure that your data member names match your JSON property names.

Post a Comment for "Pass Widget Data Using Json And Saving To Database"