0

In the directory Models I have this simple model:

public class User
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string Email { get; set; }
}

In folder Controllers, which handle request, I have this:

 {
    // PUT: /User/Edit
    [HttpPut]
    public JsonResult Edit(int id, User user)
    {
        System.Diagnostics.Debug.WriteLine("Put request is working"); //не выполняется
        return Json("Response from Edit");
    }
}

And in file Index.cshtml ajax code, which is sending the HttpPut request to controller

          /*PUT*/
          $.ajax({
              url: '/User/Edit',
              dataType: "json",
              type: "PUT",
              contentType: 'application/json; charset=utf-8',
              data: JSON.stringify({ id: 100, user: { name: 'Dmitry', email: 'dmitry@gmail.com' } }),
              async: true,
              processData: false,
              cache: false,
              success: function (data) {
                  alert(data); //this message does NOT work
              },
              error: function (xhr) {
                  alert('error');   //This message shows 
              }
          });

What should I add ? POST request with ajax, using JSON, works well, the problem is only with Put request

DanielBarbarian
  • 5,093
  • 12
  • 35
  • 44
Dmitry
  • 15
  • 1
  • 4

1 Answers1

0

The id parameter will be bound from URL, also it is not needed as you can include it directly in the User class, so make it like this:

   /*PUT*/
          $.ajax({
              url: '/User/Edit/100',
              dataType: "json",
              type: "PUT",
              contentType: 'application/json; charset=utf-8',
              data: JSON.stringify({  name: 'Dmitry', email: 'dmitry@gmail.com' }),
              async: true,
              processData: false,
              cache: false,
              success: function (data) {
                  alert(data); //this message does NOT work
              },
              error: function (xhr) {
                  alert('error');   //This message shows 
              }
      });

Or you can do it this way

  /*PUT*/
          $.ajax({
              url: '/User/Edit',
              dataType: "json",
              type: "PUT",
              contentType: 'application/json; charset=utf-8',
              data: JSON.stringify({id:100,  name: 'Dmitry', email: 'dmitry@gmail.com' }),
              async: true,
              processData: false,
              cache: false,
              success: function (data) {
                  alert(data); //this message does NOT work
              },
              error: function (xhr) {
                  alert('error');   //This message shows 
              }
      });

And the controller:

 // PUT: /User/Edit
    [HttpPut]
    public JsonResult Edit(User user)
    {
        System.Diagnostics.Debug.WriteLine("Put request is working"); //не выполняется
        return Json("Response from Edit");
    }
Haitham Shaddad
  • 4,336
  • 2
  • 14
  • 19