1

Im creating some javascript items in a loop

var licenseList = {};

$($licenses).each(function (index) {

                var license = {};
                var editedValues = {};

                license.PRODUCT_KEY = $(this).parent('div.licensewrapper').data('productkey');

                $(this).find('input:text').each(function (i, val) {

                    if ($(val).attr('data-default-value') != $(val).val() && $(val).val() > 0 && $(val).data('isValid') != false) {

                        var pogKey = $(val).data('product_option_group_key');
                        var editedValue = $(val).val();

                        editedValues[pogKey] = editedValue;

                        license.editedValues = editedValues;

                    }

                });

    //licenseList[index] = license;
    //liceneList.push(license); //if array...
});

I've commented out my current solutions. But i dont think any of the two are equal to a generic list when derelializing them in c#. Whats the corrent way to do it in this case? Thanks

2 Answers 2

3

create your array

var licenseList = [];

for each of your licenses...

var license = {
    prop1: 'p1',
    prop2: 'p2'
};
licenseList.push(license);

format and serialize JSON data to be sent over to webmethod

data = {
    LicenseList: licenseList
};

$.ajax({
      ...
      data: JSON.stringify(data)
      ...
      });

in your webmethod your method parameter must match

[WebMethod]
public static string GetLicenses(List<License> LicenseList)
{
   foreach(var license in LicenseList)
   {
     // do whatever
   }
}

sample license class. Your properties need to match with your objects in your javascript

public class License
{
   public string Prop1 {get; set;}
   public string Prop2 {get; set;}
}

Hope this helps.

Sign up to request clarification or add additional context in comments.

2 Comments

Yea i suppose this is the best way then. Btw, the properties doesnt need to match, but if they do, they get mapped to the c# object automatically.
yes you are right, i should have said in order to retrieve the values correctly.
0
  function sendToServer(licenseList)
  {
  var url = "controller.ashx";
  var data = {};
  data.SerializedObj = JSON.stringify(licenseList);
   $.post(url, data, function(response){
    if(response.length > 0)
    {
        alert(response);
    }
   });
  }

  //controller.ashx :
  public void ProcessRequest (HttpContext context) {
  //...
  string serializedObj = context.Request.Form["SerializedObj"] ?? "";
  JavaScriptSerializer js = new JavaScriptSerializer();
  List<license> collection = js.Deserialize<List<license>>(serializedObj);

  //...
  public class license
  {
  public  string Propertie1 {get;set;}
  public  string Propertie2 {get;set;}
  }

Javascript object must have the same properties:

var license = {Propertie1 : 'value1', Propertie2 : 'value2'};

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.