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...