knockout.js with optionsValue and value

Posted by Mike Flynn on Stack Overflow See other posts from Stack Overflow or by Mike Flynn
Published on 2013-06-24T21:56:15Z Indexed on 2013/06/25 4:21 UTC
Read the original article Hit count: 173

Filed under:

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 [];
            });
    }

© Stack Overflow or respective owner

Related posts about knockout.js