Click here to Skip to main content
15,904,351 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
I want to display an error message when no records found in jquery, but the error message is not displaying.

What I have tried:

<pre lang="Javascript"> function EditApi() {

        // debugger;
        var id = $("#txtaddEmpid").val();
        var name = $("#txtaddEmpName").val();
        var salary = $("#txtaddEmpDep").val();
        var age = $("#txtaddEmpAge").val();
        $.ajax({
            url: "http://localhost:53555/api/emp/" + id,
            type: "Put",
            data: JSON.stringify([name, salary, age, id]),
            contentType: 'application/json; charset=utf-8',
            success: function (data)
            {
                Console.log(data);
                alert('Updated Successfully');
                //window.location.href = "../Index";
            },

            error: function (data)
            {
                alert('No Record Found');
            }
        });
    };

C#
public void Put(List<string> emp, int ID)
      {

          Employee tt = tst.Employee.Find(ID);
          int id = Convert.ToInt32(emp[3]);
          int id1 = Convert.ToInt32(emp[2]);


              tt.Id = id;
              tt.Name = emp[1];
              tt.Salary = emp[0];
              tt.Age = id1;
              tst.SaveChanges();



      }
Posted
Updated 6-Jun-20 17:59pm
Comments
Suvendu Shekhar Giri 19-Oct-16 4:55am    
..and what is the error message?
F-ES Sitecore 19-Oct-16 5:20am    
A better way of handling this is to make your Put method return false if there is no record found rather than relying on exceptions etc.

1 solution

why don't you try HttpResponseMessage at api side? you will be easily able to return response with custom message.

Your method will be something like:
C#
public async Task<httpresponsemessage> Put(List<string> emp, int ID)
      {
 
          Employee tt = tst.Employee.Find(ID);
          if(tt == null)
          {
            return Request.CreateResponse(HttpStatusCode.BadRequest, "No Record Found");
          }
          int id = Convert.ToInt32(emp[3]);
          int id1 = Convert.ToInt32(emp[2]);
 

              tt.Id = id;
              tt.Name = emp[1];
              tt.Salary = emp[0];
              tt.Age = id1;
              tst.SaveChanges();
 
         return Request.CreateResponse(HttpStatusCode.OK, "success!");
      }

</string></httpresponsemessage>



* Mark as solution if it's helpful for you.
 
Share this answer
 
Comments
chandra sekhar 19-Oct-16 5:57am    
I have used HttpResponseMessage but im unable to see any error message in the UI
Hitesh-Systematix 19-Oct-16 6:01am    
have you checked response status in success/error callback? I am sure you will find the solution from diagnose "data.status".
chandra sekhar 19-Oct-16 6:04am    
How can i check the response??
Hitesh-Systematix 19-Oct-16 6:08am    
Try this:

success: function (data)
{
console.log('success with status:'+data.status);
},
error: function (data)
{
console.log('Error with status: '+data.status);
}
chandra sekhar 19-Oct-16 6:13am    
Thanks,it worked :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900