knockout.js with optionsValue and value
- by Mike Flynn
Is there a way to keep the value binding to the object, but have the optionsValue be a property on the object. As of now if I specify both, the optionsValue property that is selected will populate the value binding. Id like to keep the object intact in the observable, but specify what value to be set in the select list value. This way a form submit will send the optionsValue I chose.
@Html.DropDownListFor(q => q.DivisionId, new SelectList(Enumerable.Empty<string>()), new { data_bind="options: divisions, optionsText: 'Name', optionsValue: 'Id', value: division, optionsCaption: ' - '" })
function AddCrossPoolGameDialog() {
var self = this;
self.divisions = ko.observableArray([]);
self.division = ko.observable();
self.awayDivisionTeams = ko.computed(function () {
var division = ko.utils.arrayFirst(self.divisions(), function(item) {
return self.division.Name() == item.Name;
});
if (division) {
return division.DivisionTeamPools;
}
return [];
});
}