Web API Series Tutorial 3.7-Combat: Working with data (creating a UI view)

Source: Internet
Author: User

In this section, you will begin to define HTML for your app and add data bindings between the HTML and view models.

Open the views/home/index.cshtml file. Replace all the contents of the file with the following code.

@section scripts {@Scripts. Render ("~/bundles/app")}<div class="Page-header">  <H1>Bookservice</H1></div><div class="Row">  <div class="col-md-4">    <div class="Panel panel-default">      <div class="panel-heading">        <h2 class="Panel-title">Books</H2>      </div>      <div class="Panel-body">        <ul class="list-unstyled" data-bind="Foreach:books">          <li>            <strong><span data-bind="Text:authorname"></span></Strong>:<span data-bind="Text:title"></span>            <small><a href="#">Details</a></Small>          </li>        </ul>      </div>    </div>    <div class="alert Alert-danger" data-bind="Visible:error"> <p data-bind="Text:error"></P></div>  </div>  <div class="col-md-4">    <!--todo:book details --  </div>  <div class="col-md-4">    <!--todo:add New Book--  </div></div>

Most of the DIV elements here are bootstrap style, which is an important element with data binding properties. This element links HTML to the view model.

For example:

<data-bind="text: error">

In this example, the "text" binding causes the P element to display the value of the error property in the view model. The callback for error is declared in ko.obserable:

self.error = ko.observable(); 

Whenever a new value is modified to error, knockout updates the text on the < p > element.

The foreach binding tells knockout to iterate through the books array. For each item of the array, knockout creates a new < li > element. Binds the context of a foreach reference to each item of an array. For example:

<span data-bind="text: Author"></span>

Here is a text binding that reads the Author property of each book.

If you run the application now, it will look like this:

After the page is loaded, the books list is loaded asynchronously. Now, the Details link is not yet functional. We'll add this feature to it in the next section.

Web API Series Tutorial 3.7-Combat: Working with data (creating a UI view)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.