Suppose we have a ViewModel like this
The markup to populate HTML Select should be
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
model.user = UserManager.GetUserByID(id); | |
model.locations = Location.GetLocations(); |
The markup to populate HTML Select should be
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<p data-bind="with: user"> | |
Your country: | |
<select data-bind="options: $root.availableLocations, | |
optionsText: 'Text', | |
optionsValue: 'Value', | |
value: LocationID, | |
optionsCaption: 'Choose...'"> | |
</select> | |
</p> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var userObject = @Html.Raw(Json.Encode(Model.user)); | |
var locationsArray = @Html.Raw(Json.Encode(Model.locations)); | |
var vm = { | |
user: ko.observable(userObject), | |
availableLocations: ko.observableArray(locationsArray) | |
}; | |
ko.applyBindings(vm); |
1 comments on "Binding html select using knockoutjs and asp.net mvc"
Subscribe in a Reader
I thought it was going to be some boring old post, but it really compensated for my time. front end developers
Post a Comment