Forming a model and using computed properties

In addition to having the opportunity to bind interface elements to specific model properties, you can also bind them to expressions. Let's look at some examples:

Razor: <button type="submit" @ko.Bind.Enable(m => m.StringValue.Length > 0)>Add</button> Html: <button type="submit" data-bind="enable : StringValue().length>0">Add</button> Razor: @ko.Bind.Enable(m => m.Items.Count > 0) Html: data-bind="enable : Items().length>0" Razor: @using (ko.If(model => model.Condition1 && model.Condition2)) Html: <!-- ko if: Condition1()&&Condition2() --> Razor: @ko.Html.Span(m => m.Price).Style("color", m => m.Price > 0 ? "black" : "red") Html: <span data-bind="text : Price,style : {color : Price()>0 ? 'black' : 'red'}"></span> ...

Get Getting Started with Knockout.js for .NET Developers now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.