In this article I will explain with an example, how to create and consume a Web API PUT Action method in ASP.Net MVC Razor.
HTTP PUT method is used to update an existing record in the data in the RESTful API.
This article will illustrate how to create a PUT Action method in Web API and use it for updating records in Database using Entity Framework in ASP.Net MVC Razor.
Note: For beginners in ASP.Net MVC and Web API, please refer my article: What is Web API, why to use it and how to use it in ASP.Net MVC for details about using and configuring Web API.
 
 
Database
I have made use of the following table Customers with the schema as follows. CustomerId is an Auto-Increment (Identity) column.
Create and consume Web API PUT method in ASP.Net MVC
 
I have already inserted few records in the table.
Create and consume Web API PUT method in ASP.Net MVC
 
Note: You can download the database table SQL by clicking the download link below.
          Download SQL file
 
 

Entity Framework Model
Once the Entity Framework is configured and connected to the database table, the Model will look as shown below.
Note: For beginners in ASP.Net MVC and Entity Framework, please refer my article ASP.Net MVC: Simple Entity Framework Tutorial with example. It covers all the information needed for connecting and configuring Entity Framework.
 
Create and consume Web API PUT method in ASP.Net MVC
 
 
Controller
The Controller consists of the following Action method.
Action method for handling GET operation
Inside this Action method, simply the View is returned.
public class HomeController : Controller
{
    // GET: Home
    public ActionResult Index()
    {
        return View();
    }
}
 
 
Web API Controller
The Web API Controller consists of the following Action method.
 
Action method for Updating
Inside this Action method, the Customer object is received as parameter. The CustomerId value of the received Customer object is used to reference the Customer record in the Customer Entities.
If the record is found, the values of Name and Country are updated and the changes are updated into the Customers table and a Boolean value TRUE is returned.
If the record is not found, then a Boolean value FALSE is returned.
public class AjaxAPIController : ApiController
{
    [Route("api/AjaxAPI/UpdateCustomer")]
    [HttpPost]
    public bool UpdateCustomer(Customer _customer)
    {
        using (CustomersEntities entities = new CustomersEntities())
        {
            Customer updatedCustomer = (from c in entities.Customers
                                        where c.CustomerId == _customer.CustomerId
                                        select c).FirstOrDefault();
 
            if (updatedCustomer != null)
            {
                updatedCustomer.Name = _customer.Name;
                updatedCustomer.Country = _customer.Country;
                entities.SaveChanges();
                return true;
            }
        }
 
        return false;
    }
}
 
 
View
The View consists of two TextBoxes and a Button. The Button has been assigned with a jQuery Click event handler.
When the Update button is clicked, the values of CustomerId, Name and Country are passed to the UpdateCustomer Action method using jQuery AJAX call.
Once the response is received, based on whether response is TRUE or FALSE, appropriate message is displayed using JavaScript Alert Message Box.
@{
    Layout = null;
}
 
<!DOCTYPE html>
 
<html>
<head>
    <meta name="viewport" content="width=device-width"/>
    <title>Index</title>
</head>
<body>
    <table border="0" cellpadding="0" cellspacing="0">
        <tr>
            <td style="width: 60px">
                Id<br/>
                <input type="text" id="txtCustomerId" style="width:50px"/>
            </td>
            <td style="width: 150px">
                Name<br/>
                <input type="text" id="txtName" style="width:140px"/>
            </td>
            <td style="width: 150px">
                Country:<br/>
                <input type="text" id="txtCountry" style="width:140px"/>
            </td>
            <td style="width: 200px">
                <br/>
                <input type="button" id="btnUpdate" value="Update"/>
            </td>
        </tr>
    </table>
    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
    <script type="text/javascript" src="https://ajax.cdnjs.com/ajax/libs/json2/20110223/json2.js"></script>
    <script type="text/javascript">
        $("body").on("click", "#btnUpdate", function () {
            var _customer = {};
            _customer.CustomerId = $("#txtCustomerId").val();
            _customer.Name = $("#txtName").val();
            _customer.Country = $("#txtCountry").val();
            $.ajax({
                type: "POST",
                url: "/api/AjaxAPI/UpdateCustomer",
                data: JSON.stringify(_customer),
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function (r) {
                    if (r) {
                        alert("Customer record updated.");
                    } else {
                        alert("Customer not found.");
                    }
                }
            });
        });
    </script>
</body>
</html>
 
 
Screenshot
Create and consume Web API PUT method in ASP.Net MVC
 
 
Downloads