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

Inner computed properties


Knockout MVC also supports simple computed properties. These properties have a JavaScript representation and can be calculated on the client without additional requests to the server. For example, let's define a computed property DisplayText for our BookModel. This property will be calculated on the basis of Author and Title.

The Model will be as follows:

public class BookModel
{
  public string Title { get; set; }
  public string Author { get; set; }

  [Computed]
  [ScriptIgnore]
  [JsonIgnore]
  public string DisplayText
  {
    get { return Author + ": " + Title; }
  }
}
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" },
        new BookModel { Title...