JParmar
JParmar

Reputation: 89

Pass Multiple Parameters from Jquery to ASPX web method not working

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)
        {

        }

Upvotes: 1

Views: 334

Answers (1)

panoskarajohn
panoskarajohn

Reputation: 1988

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.

Upvotes: 1

Related Questions