I'm trying to bind a checkboxlist using knockoutjs. I have a user related to a list of profiles, what I want is when the page is loaded to init the checkboxlist with the user profiles.
This is my code
var userProfileList = [];
var jsonUserAdminModel = null;
var viewModel = {
,firstName: ko.observable()
,lastName: ko.observable()
,userId: ko.observable()
,userProfiles: ko.observableArray(userProfileList)
,result: ko.observable()
};
viewModel.result = ko.computed(function () {
jsonUserAdminModel = {
UserId: (this.userId() != undefined ? this.userId() : null)
,FirstName: (this.firstName() != undefined ? this.firstName() : null)
,LastName: (this.lastName() != undefined ? this.lastName() : null)
}
return jsonUserAdminModel;
}, viewModel);
$(document).ready(function () {
ko.applyBindings(viewModel);
//Init the list of all profiles
viewModel.userProfiles(@Html.Raw(Json.Encode(Model.Profiles)));
});
HTML
<div id="UserProfiles" data-bind="foreach: userProfiles" style="margin: 10px">
<input type="checkbox" data-bind="value: ProfileId" /><span data-bind="text: Name"></span>
</div>
I want to check the user default profiles and bind it.
Thanks.