鍍金池/ 教程/ C#/ 在本節(jié),你將添加讓用戶可以創(chuàng)建新book的功能。在app.js中,添加如下代碼到視圖模型:
前言
在本節(jié),你將開始為app定義HTML,并在HTML和視圖模型間添加數(shù)據(jù)綁定。
在本節(jié),你將使用HTML、JavaScript和Knockout.js庫為應(yīng)用程序創(chuàng)建客戶端。我們將按如下步驟建立客戶端應(yīng)用:
前言
前言
http://www.asp.net/web-api/overview/formats-and-model-binding/mo
在本節(jié),你將添加讓用戶可以創(chuàng)建新book的功能。在app.js中,添加如下代碼到視圖模型:
在本節(jié),你將添加查看每本書的詳細信息的功能。在app.js中,添加以下代碼到視圖模型:
前言
這篇文章描述了ASP.NET Web API如何將HTTP請求發(fā)送(路由)到控制器。
前言
前言
這篇文章描述了ASP.NET Web API如何將HTTP請求路由到控制器上的特定動作。
前言
在這最后一節(jié)中,你將把應(yīng)用程序發(fā)布到Azure。在Solution Explorer中,右擊項目并選擇Publish。
前言
總結(jié)

在本節(jié),你將添加讓用戶可以創(chuàng)建新book的功能。在app.js中,添加如下代碼到視圖模型:

在本節(jié),你將添加讓用戶可以創(chuàng)建新book的功能。在app.js中,添加如下代碼到視圖模型:

self.authors = ko.observableArray();
self.newBook = {
    Author: ko.observable(),
    Genre: ko.observable(),
    Price: ko.observable(),
    Title: ko.observable(),
    Year: ko.observable()
}

var authorsUri = '/api/authors/';

function getAuthors() {
    ajaxHelper(authorsUri, 'GET').done(function (data) {
        self.authors(data);
    });
}

self.addBook = function (formElement) {
    var book = {
        AuthorId: self.newBook.Author().Id,
        Genre: self.newBook.Genre(),
        Price: self.newBook.Price(),
        Title: self.newBook.Title(),
        Year: self.newBook.Year()
    };

    ajaxHelper(booksUri, 'POST', book).done(function (item) {
        self.books.push(item);
    });
}

getAuthors();

在Index.cshtml中,替換以下代碼:

<div class="col-md-4">
    <!-- TODO: Add new book -->
</div>

到:

<div class="col-md-4">
<div class="panel panel-default">
  <div class="panel-heading">
    <h2 class="panel-title">Add Book</h2>
  </div>

  <div class="panel-body">
    <form class="form-horizontal" data-bind="submit: addBook">
      <div class="form-group">
        <label for="inputAuthor" class="col-sm-2 control-label">Author</label>
        <div class="col-sm-10">
          <select data-bind="options:authors, optionsText: 'Name', value: newBook.Author"></select>
        </div>
      </div>

      <div class="form-group" data-bind="with: newBook">
        <label for="inputTitle" class="col-sm-2 control-label">Title</label>
        <div class="col-sm-10">
          <input type="text" class="form-control" id="inputTitle" data-bind="value:Title"/>
        </div>

        <label for="inputYear" class="col-sm-2 control-label">Year</label>
        <div class="col-sm-10">
          <input type="number" class="form-control" id="inputYear" data-bind="value:Year"/>
        </div>

        <label for="inputGenre" class="col-sm-2 control-label">Genre</label>
        <div class="col-sm-10">
          <input type="text" class="form-control" id="inputGenre" data-bind="value:Genre"/>
        </div>

        <label for="inputPrice" class="col-sm-2 control-label">Price</label>
        <div class="col-sm-10">
          <input type="number" step="any" class="form-control" id="inputPrice" data-bind="value:Price"/>
        </div>
      </div>
      <button type="submit" class="btn btn-default">Submit</button>
    </form>
  </div>
</div>
</div>

這段代碼創(chuàng)建了一個表單,用于提交新的作者。作者下拉框的值被數(shù)據(jù)綁定到視圖模型的authors中。對于其他的表單輸入,這些值都被數(shù)據(jù)綁定到視圖模型的newBook屬性。

這個表單上的提交事件被數(shù)據(jù)綁定到addBook函數(shù):

<form class="form-horizontal" data-bind="submit: addBook">

這個addBook函數(shù)讀取數(shù)據(jù)綁定表單輸入中的當(dāng)前值,并創(chuàng)建JSON對象。然后會POST這個JSON對象到/api/books。