| uid | web-api/overview/data/using-web-api-with-entity-framework/part-9 |
|---|---|
| title | Add a New Item to the Database | Microsoft Docs |
| author | Rick-Anderson |
| description | Describes how to add the functionality that allows users to add new books to the book service application. |
| ms.author | tdykstra |
| ms.date | 06/16/2014 |
| ms.assetid | 0967c29e-e124-4db0-a788-c45d0ff5aff2 |
| msc.legacyurl | /web-api/overview/data/using-web-api-with-entity-framework/part-9 |
| msc.type | authoredcontent |
In this section, you will add the ability for users to create a new book. In app.js, add the following code to the view model:
[!code-javascriptMain]
In Index.cshtml, replace the following markup:
[!code-htmlMain]
With:
[!code-htmlMain]
This markup creates a form for submitting a new author. The values for the author drop-down list are data-bound to the authors observable in the view model. For the other form inputs, the values are data-bound to the newBook property of the view model.
The submit handler on the form is bound to the addBook function:
[!code-htmlMain]
The addBook function reads the current values of the data-bound form inputs to create a JSON object. Then it POSTs the JSON object to /api/books.