Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6165579
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T22:11:25+00:00 2026-05-23T22:11:25+00:00

I am binding a list of objects to a select using knockout. Object Class

  • 0

I am binding a list of objects to a select using knockout. Object Class can have any number of properties

<select id="TheProperty_City" 
        name="TheProperty_City" 
        class="required" 
        data-bind="options: cityList, 
                   optionsText: 'Name',  
                   value: selectedCity, 
                   optionsCaption: '--select the city--'" />

This works perfectly fine and I can use viewModel.selectedCity().Name or viewModel.selectedCity().Value for loading child elements.

My issue is with jQuery validation. If I leave the statement as above, jQuery does not reset the error even after selection.

I fixed it with by specifying the optionsValue in the bind, but then the selectedCity returns the scalar value and not the entire object. Any idea how to preserve the object behavior or do the validation differently?

 <select id="TheProperty_City" 
         name="TheProperty_City" 
         class="required" 
         data-bind="options: cityList, 
                    optionsText: 'Name',  
                    optionsValue: 'Value', //added the optionsValue
                    value: selectedCity, 
                    optionsCaption: '--select the city--'" />

The error stays there when optionsValue is not specified:

Without the OptionsValue

Here’s my Object Watch on selectedCity:

viewModel.selectedCity() in watch returns an object

Here’s an Object Watch on selectedCity when optionsValue is specified:

With OptionsValue

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T22:11:27+00:00Added an answer on May 23, 2026 at 10:11 pm

    The issue is that when dealing with objects as the value, the option elements have their value set to “”. The jQuery validation fails because of this. You could write a binding or wrapper binding to the options binding that goes through and just sets them to a value, but I don’t think that it is preferable to go that route.

    A decent option is to store the value and use a dependentObservable to represent the currently selected object.

    It would be like:

    var viewModel = {
        cityList: [{ Name: "Madison", Value: "MSN" }, { Name: "Milwaukee", Value: "MKE" }, { Name: "Green Bay", Value: "GRB" }],
        selectedCityValue: ko.observable()
    };
    
    viewModel.selectedCity = ko.dependentObservable(function() {
        var value = this.selectedCityValue();
        return ko.utils.arrayFirst(this.cityList, function(city) {
           return city.Value === value; 
        });
    }, viewModel);
    

    With a binding like:

    <select id="TheProperty_City" name="TheProperty_City" class="required" 
        data-bind="options: cityList, 
        optionsText: 'Name', 
        optionsValue: 'Value',
        value: selectedCityValue, 
        optionsCaption: '--select the city--'" />
    

    Sample here: http://jsfiddle.net/rniemeyer/EgCM3/

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Binding a List collection to a datagrid. How can you limit what properties will
Concerning data binding I have these classes: public class Foo : List<Bar> { public
Background I have a payment page where the user can select from a list
I have custom objects I am binding to using ObjectDataSource. I have three-level binding:
I'm using the options binding on a select list used in a jQuery template:
Can anyone explain why I can rebind list but not +? (binding [list vector]
I am binding a List to a DataGridView. One property of the SomeObject class
Is there a better way of binding a list of base class to a
I have a List (Of MyObject) binding to a GridView in the Page_Load event.
I have datagrid with list of MyPlayer objects linked to ItemsSource, there are ComboBoxes

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.