1

I have one array which contain value in following format

var checkedvalue = [];
var x = "230";//Just now constant value pass
var y = "450";
checkedvalue.push({
     xCordi : x, 
     yCordi : y,
     bPart:txtName.value
});

in my code i have two button, 1 for push the TEXTBOX data in my array.
Now on second button click i want to pass this value to my web method and perform operation.

 $AddtoDB.on('click', function () {
                    if ( $("#<%= divdynamicData.ClientID %>").is(':empty')){
                    alert("No Data Found");
                    }
                  else{
                    var ImageName="test.jpeg";
                    var ImagePath ="c:\drive";
                    IndertIntoDB(ImageName,ImagePath,checkedvalue);
                  }
            });

the function is called when there is data.

function IndertIntoDB(ImageName,ImagePath,checkedvalue){
         //alert(ImageName);
         //alert(ImagePath);
         //$.each( checkedvalue, function( key, value ) {  alert( value.xCordi + ": " + value.yCordi +":"+ value.bPart );});
         $.ajax({
            type: "POST",
            url: "ImageHotSpot.aspx/GetSaveData",
            data: JSON.stringify({ iName: ImageName,iPath:ImagePath,iData:checkedvalue }),
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (data) {
                alert("DataInserted");
        }

    });

Now i want to iterate the checkarray in web method but i got error.

  //}
        [System.Web.Services.WebMethod]
        public static void GetSaveData(string iName, string iPath, string[] iData)
        {

        }

3
  • Yout issue is with iData which is an array of a js object and not an array of strings. Fix this and your post request will work. Commented Feb 8, 2020 at 19:32
  • @panoskarajohn i have declare variable in this format var checkedvalue = []; Commented Feb 8, 2020 at 19:37
  • Yes js does not to declare what type will be in the array. You push an anonymous object with three properties xCordi, yCordi, bPart. So you need a similar object in C# with the same three properties. The name of the class in C# is irrelevant but the three properties should be the same name. Then c# will be able to deserialize your object. Commented Feb 8, 2020 at 19:41

1 Answer 1

1

This answer is at first glance since i was not able to extract more info.

I hope this helps.

This is what your checkedvalue variable looks like in js code.

var checkedvalue = [];
var x = "230";//Just now constant value pass
var y = "450";
checkedvalue.push({
     xCordi : x, 
     yCordi : y,
     bPart:txtName.value
});

And this is your ajax request data.

data: JSON.stringify({ iName: ImageName,iPath:ImagePath,iData:checkedvalue })

Here is the method that is called with ajax.

public static void GetSaveData(string iName, string iPath, string[] iData)

Now i am not very familiar with ajax calling aspx. Usually there is a controller with MVC.

Since you don't mention the error in the original post.

My guess is you get a Null reference exception with checkedvalue->iData. C# cannot deserialize a complex object from javascript without having the specifics. Meaning a c# object, it passes null.

An array of strings would work like this.

var checkedvalue = [];
checkedvalue.push("Hello"); //etc push other strings

You need a c# class like this one. Assuming they are of type string all three. From context i believe xCordi, yCordi are of type int feel free to change them. Also remove the "" from your js code.

public class SomeClass{
     public string xCordi {get;set;} //change string to int
     public string yCordi {get;set;} //change string to int
     public string bPart {get;set;}
}

public static void GetSaveData(string iName, string iPath, string[] iData) -> public static void GetSaveData(string iName, string iPath, List<SomeClass> iData) or public static void GetSaveData(string iName, string iPath, SomeClass[] iData)

Then your ajax method should work and c# should be able to deserialize the object.

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.