uid | title | author | description | ms.author | ms.date | ms.assetid | msc.legacyurl | msc.type |
---|---|---|---|---|---|---|---|---|
web-api/overview/data/using-web-api-with-entity-framework/part-9 |
Add a New Item to the Database | Microsoft Docs |
Rick-Anderson |
Describes how to add the functionality that allows users to add new books to the book service application. |
riande |
06/16/2014 |
0967c29e-e124-4db0-a788-c45d0ff5aff2 |
/web-api/overview/data/using-web-api-with-entity-framework/part-9 |
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
.