Book Image

Getting Started with Knockout.js for .NET Developers

By : Andrey Ankshin
Book Image

Getting Started with Knockout.js for .NET Developers

By: Andrey Ankshin

Overview of this book

Table of Contents (14 chapters)
Getting Started with Knockout.js for .NET Developers
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Complex bindings


In some cases, we need to apply several bindings to a single element. This is easy to do; you just need to concatenate on the target binding methods. Let's update the previous example with the book read status. First, we set the binding for the status text with the Text method. Next, we set the color of the status: green for read books and red for unread books.

The Model will be as follows:

public class BookModel
{
  public string Title { get; set; }
  public string Author { get; set; }
  public int Year { get; set; }
  public bool Read { get; set; }
}
public class LibraryModel
{
  public List<BookModel> Books { get; set; }
}

The Controller will be as follows:

public class LibraryController : KnockoutController
{
  public ActionResult Index()
  {
    var model = new LibraryModel
    {
      Books = new List<BookModel>
    {
        new BookModel { Title = "Oliver Twist", Author = "Charles Dickens", Year = 1837, Read = true },
        new BookModel { Title = "Winnie...