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

Regions


Knockout MVC regions provide you with the Razor API to use Knockout.js control flow binding, such as if, foreach, and with. In this section, you will learn how to create these elements without JavaScript code.

The foreach region

The foreach region allows you to iterate over some collections with nice Razor syntax. Let's implement our usual library example in Knockout MVC:

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 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 },
          new BookModel { Title = "Winnie-the-Pooh", Author = "A. A. Milne", Year...