3

I am doing a update operation from my web method. What I am doing is I have two text boxes inside my webForm1.aspx page. I am trying to post my these textboxes values to web method so my update operation will run. Below is my code:

var uval1 = $("#up_tb1").val();
var uval2 = $("#up_tb2").val();
function upbtnclicked() {
        alert('clicked');
        $.ajax({
            type: "POST",
            url: "WebForm1.aspx/updateData",
            data: '{val1:"' + uval1 + '",val2:"' + uval2 + '"}',
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: OnSuccess,
            error: OnErrorCall
        });

        function OnSuccess(response) {
            alert(response.d);
        }
        function OnErrorCall(response) { console.log(error); }
    }

My update procedure is running fine but when I put debugging point on my web method and check parameters values it contain undefined values and don't getting correct values from text boxes. below is my codebehind code. Please help me here.

[WebMethod]
    public static bool updateData(string val1,string val2)
    {
        var db = new dbDataContext();
        var query = (from e in db.Employees
                     where e.ID == up_id
                     select e).FirstOrDefault();
        query.EMP_FNAME = val1;
        query.EMP_MNAME = val2;
        db.SubmitChanges();
        return true;    
    }
Ahmer Ali Ahsan
  • 5,636
  • 16
  • 44
  • 81
  • your approach is wrong. do like this `data: {val1: uval1 ,val2: uval2 },` – anand Feb 04 '16 at 10:02
  • In general, don't try and build JSON by hand; you'll probably do it wrong. In this case, jQuery will convert the value of `data` for you. – Heretic Monkey Sep 23 '21 at 23:08
  • Does this answer your question? [Pass Multiple Parameters to jQuery ajax call](https://stackoverflow.com/questions/1916309/pass-multiple-parameters-to-jquery-ajax-call) – Heretic Monkey Sep 23 '21 at 23:10

2 Answers2

0

You need to send an array from the data parameter in ajax. Try something like:

var uval1 = $("#up_tb1").val();
var uval2 = $("#up_tb2").val();
function upbtnclicked() {
        alert('clicked');
        $.ajax({
            type: "POST",
            url: "WebForm1.aspx/updateData",
            data: {val1: uval1 ,val2: uval2 },
            contentType: "application/json; charset=utf-8",
            success: OnSuccess,
            error: OnErrorCall
        });

        function OnSuccess(response) {
            alert(response.d);
        }
        function OnErrorCall(response) { console.log(error); }
    }
Zippy
  • 1,804
  • 5
  • 27
  • 36
  • You means like this `var Values = new Array(); Values[0] = $("#up_tb1").val(); Values[1] = $("#up_tb2").val();` – Ahmer Ali Ahsan Feb 04 '16 at 10:43
  • No, what you're sending is a string, using `{val1: uval1, val2:uval2}` would send an array which contains `val1` and `val2`. And in the `webmethod` you'll receive them mapped with the parameters required – Zippy Feb 04 '16 at 10:46
  • Also, you said that you get undefined values, could you update your question with the textboxes definitions? – Zippy Feb 04 '16 at 10:47
  • I completely describe my question in my post you should re check it. – Ahmer Ali Ahsan Feb 04 '16 at 11:03
  • Please show the html definion of the textboxes (That's what i was asking for). Sorry if i wasn't more explicit. – Zippy Feb 04 '16 at 11:05
  • There's no array there. Array literals in JavaScript are denoted by `[]`, not `{}`, which is reserved for object literals. – Heretic Monkey Sep 23 '21 at 23:11
0
var uval1 = $("#up_tb1").val();
var uval2 = $("#up_tb2").val();
function upbtnclicked() {
    alert('clicked');
    $.ajax({
        type: "POST",
        url: "WebForm1.aspx/updateData",
        data: {val1: uval1 ,val2: uval2 },
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: OnSuccess,
        error: OnErrorCall
    });

    function OnSuccess(response) {
        alert(response.d);
    }
    function OnErrorCall(response) { console.log(error); }
}

and you must have to ensure that passed parameters must match webmethod params in server side.

[WebMethod]
public static bool updateData(string val1,string val2)
{
    using (var db = new dbDataContext())
    {
        var query = (from e in db.Employees
                     where e.ID == up_id
                     select e).FirstOrDefault();
        query.EMP_FNAME = val1;
        query.EMP_MNAME = val2;
        db.SubmitChanges();
    }
    return true;    
}
Farruk
  • 31
  • 1
  • 9