printthis
printthis

Reputation: 151

Bind ASP.NET MVC model value to checkbox with knockout.js

I am new to knockout.js. I have a Y/N value from my model that I want to bind to a checkbox.

This is my view:

<tbody id="tblMultiEdit" data-bind="foreach: UUTs">
<tr>
<td data-bind="text: SerialNumber"></td>
<td><input type="checkbox" data-bind="ReqDowngrade" /></td>
<td><input type="checkbox" data-bind="ACTSupported"/></td>
<td><input type="checkbox" data-bind="ProdModeOff"/></td>
</tr>
</tbody>

Knockout code:

function ViewModel(UUTs) 
{

var self = this;

self.UUTs = UUTs;

};

var viewModel = new ViewModel(@Html.HtmlConvertToJson(Model));

ko.applyBindings(viewModel);

So far I can bind the text with no problem, but checkboxes are in blank.

Upvotes: 0

Views: 615

Answers (1)

printthis
printthis

Reputation: 151

I was able to resolve this by adding a ternary operation.

<td><input type="checkbox" data-bind="checked: (ReqDowngrade == 'Y' ? 1 : 0)" /></td>

If anybody knows a more efficient way using knockout let me know!

Upvotes: 1

Related Questions