Using Ajax method call

Total Post:154

Points:1086
 1011  View(s)
Ratings:
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?

  1. Post:378

    Points:2694
    Re: Using Ajax method call

    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;
      }
    }

      Modified On Apr-06-2018 06:45:25 AM

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!