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

Sending parameters to the server


One of the common tasks in the development of responsive applications is the AJAX execution of a controller method. Sometimes, we need to execute the method with some parameters (or method arguments). Let's consider a simplified version of the library example; we will keep only the number of books. In the following example, the business model needs three methods: the addition of one book, the addition of two books, and the addition of three books. Of course, we can implement three different methods for these tasks. However, a single method with an argument is a more flexible approach.

The Model will be as follows:

public class LibraryModel
{
  public int AmountOfBooks { get; set; }

  public void AddBooks(int count)
  {
    AmountOfBooks += count;
  }
}

The Controller will be as follows:

public class LibraryController : KnockoutController
{
  public ActionResult Index()
  {
    return View(new LibraryModel());
  }

  public ActionResult AddBooks(LibraryModel...