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 7854009
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T19:53:42+00:00 2026-06-02T19:53:42+00:00

I am failing with Knockout select list binding when using an object as a

  • 0

I am failing with Knockout select list binding when using an object as a select list value. It works fine if I use a string, but I want to bind objects.

I have a Gift object and it has a Title, Price and Company. I have a select list of companies and each company has an Id and Name. The initial selection however is not the correct in the select list.

Please see fiddle: http://jsfiddle.net/mrfunnel/SaepM/

This is important to me when binding to MVC3 view models. Though I admit it may be because I am doing things the wrong way.

If I have the following model:

public class Company
{
    public Guid Id { get; set; }
    public string Name { get; set; }

}
public class GiftModel
{
    public Company Company { get; set; }
    public string Title { get; set; }
    public double Price { get; set; }
}

How do I select a Company that is bindable in my controller? Do I need to add a CompanyId property to the GiftModel and bind to that or write custom binder. Am I missing something fundamental here?

Thanks in advance.

  • 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-06-02T19:53:45+00:00Added an answer on June 2, 2026 at 7:53 pm

    You need to do a lot of stuff.

    An CompanyId in your ViewModel is the only way to bind and make this observable.
    You can not make a object observable only it´s values

    <form class="giftListEditor" data-bind="submit: save">
        <!-- Our other UI elements, including the table and ‘add’ button, go here -->
    
        <p>You have asked for <span data-bind="text: gifts().length">&nbsp;</span> gift(s)</p>
        <table>
            <tbody  data-bind="foreach: gifts">
                <tr>
                    <td>Gift name: <input data-bind="value: Title"/></td>
                    <td>Price: $ <input data-bind="value: Price"/></td>
                    <td>Company: <select data-bind="options: $root.companies, optionsText: 'Name', optionsValue: 'Id', value: CompanyId"/></td>
                    <td>CompanyId: <span data-bind="text: CompanyId"></span></td>
                    <td><a href="#" data-bind="click: $root.removeGift">Delete</a></td>
                </tr>
            </tbody>
        </table>
        <button data-bind="click: addGift">Add Gift</button>
        <button data-bind="enable: gifts().length > 0" type="submit">Submit</button>
    </form>​
    

    your model

    // Fake data
    var initialData = [
        { Title: ko.observable('Item 1'), Price: ko.observable(56), CompanyId: ko.observable(1) },
        { Title: ko.observable('Item 2'), Price: ko.observable(60), CompanyId: ko.observable(2) }, 
        { Title: ko.observable('Item 3'), Price: ko.observable(70), CompanyId: ko.observable(2) }
    ];
    
    var initialCompanies = [
        { Id: 1, Name: "Comp 1" },
        { Id: 2, Name: "Comp 2" },
        { Id: 3, Name: "Comp 3" }
    ];
    
    var viewModel = {
        gifts: ko.observableArray(initialData),
        companies: initialCompanies,
    
        addGift: function() {
            this.gifts.push({
                Title: "",
                Price: "",
                Company: { Id: "", Name: "" }
            });
        },
        removeGift: function($gift) {
            viewModel.gifts.remove($gift);
        },
        save: function() {
            console.log(ko.toJS(viewModel.gifts));
        }
    };
    
    ko.applyBindings(viewModel, document.body);​
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is failing: List<String> names = new ArrayList<String>(); names.add(sold); Query query = em.createQuery(FROM PropField
FREMOVE is failing with an error ORA-29280: invalid directory path It works fine when
My query keeps failing to fetch. It works just fine in my SQL Console
My Google fu is failing me. How do I use Perl to serve up
EDIT jQuery.inArray kept failing to do string != int comparisons, and this was resolved
maybe I'm just failing in a really simple thing but I'm developing an intepreter
I'm trying and failing to use an ExpressionVisitor to modify an expression that calls
I'm failing to send a XML file named filesystem.xml to CouchDB using curl on
I'm failing at creating a script for my .htaccess file. I want the following
The following regex is failing on me; [Aa][æ-åÆ-Å].* But this regex is not failing

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.