O'Reilly logo

Getting Started with Knockout.js for .NET Developers by Andrey Akinshin

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required