Home > DeveloperSection > Forums > Using Ajax method call
Manoj Bhatt
Manoj Bhatt

Total Post:153

Points:1079
Posted on    November-17-2014 11:38 PM

 ASP.Net ASP.Net 
Ratings:


 1 Reply(s)
 472  View(s)
Rate this:

i have this aspx view :

 

 jQuery('#addjob').click(function () {

            if ($('#metier').val().length > 0) {

                $('.taglist').append('<li style="line-height: 20px"><a href="" style="width:250px" >' + $('#metier').val() + '<span class="icon-remove"></span></a></li>');

                $.ajax({

                    type: "POST",

                    url: "JobsEdition.aspx/AjouterMetier",

                    data: "{'job': '" + $('#metier').val() + "'}",

                    success: function (msg) {

                        AjaxSucceeded(msg);

                    },

                    error: AjaxFailed

                });

                                              }

            $('#metier').val('');

        });

In the code behin i put this method :

 

 public partial class JobsEdition : System.Web.UI.Page

    {

      List<string> jobs = new List<string>();

        [WebMethod()]

        public void AjouterMetier(string job)

        {

            if (job != "")

            {

                jobs.Add(job);

                Session["jobs"] = jobs;

            }

 

        }

}

I'm beginner in the use of ajax in Asp.net, so i had always the session variables Session["jobs"] null and the method AjouterMetier was never reached .

 

What is the error that I commited?

How can i fix it?



Sumit Kesarwani

Total Post:378

Points:2694
Posted on    November-17-2014 11:47 PM

Hi Manoj, 

Since your adding items to the session:

 

Session["jobs"] = jobs;

You might want to initialize the variable by grabbing it from session prior. For instance:

 

public void AjouterMetier(string job)

{

  if (job != "")

  {

    jobs = (List<string>)Session["jobs"] ;

    if( jobs == null )

      jobs = new List<string>();

 

    jobs.Add(job);

    Session["jobs"] = jobs;

  }

}


Don't want to miss updates? Please click the below button!

Follow MindStick