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

  • Home
  • SEARCH
  • 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 8960789
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T15:44:16+00:00 2026-06-15T15:44:16+00:00

I have a particularly tricky problem with ko. I’ll try to explain around some

  • 0

I have a particularly tricky problem with ko. I’ll try to explain around some example stuff.

First, here’s the html junk I’m trying to render to. The foreach variable “Appliances” is an array and is a property of the viewModel. The option data is also a property array of the view model. The name of that here is ApplianceTypes.

 <table>   
 <tbody data-bind="foreach: Appliances">
        <tr>
             <td>
                  <select data-bind="options: $root.ApplianceTypes, value: $data.ApplianceType.Id, optionsValue: 'Id', optionsText: 'ApplianceTypeDesc'">
                  </select>
             </td>
             <td>
                  <input data-bind="value: UnitNumber" />
             </td>                
        </tr>
 </tbody>
 </table>

Ok, so now that we have that in mind, here’s the data to fill out ideas a bit better:

 var viewModel = {
    RequestNumber:37,
    UnitNumber:3,
    Appliances:[{
        Id:2,
        ApplianceType:{Id:5, ApplianceTypeDesc:"Water Heating Tankless"},
        ServiceApplianceQuantity:"1",
    }],
    ApplianceTypes:[
        {Id:5, ApplianceTypeDesc:"Water Heating Tankless"},
        {Id:1, ApplianceTypeDesc:"Dryer"},
        {Id:2, ApplianceTypeDesc:"Range"},
        {Id:3, ApplianceTypeDesc:"Heating"},
        {Id:4, ApplianceTypeDesc:"Water Heating"},
        {Id:6, ApplianceTypeDesc:"Fireplace"},
        {Id:7, ApplianceTypeDesc:"Make Up Air Heating"},
        {Id:8, ApplianceTypeDesc:"BBQ"}
    ]
    }

So, there’s no big problem here yet. I get a UI with a row for each appliance. Each row is a type dropdown filled with appliance types. If I’m just editing what I have, viewModel.Appliances[i].ApplianceType.Id updates and life is good. If I try to add new to my appliances array, the applianceType.Id doesn’t get updated. Here’s the code for that:

function ApplianceTypeViewModel() {
var self = this;
self.Id = ko.observable("5");
self.ApplianceTypeDesc = ko.observable("");
};

function ApplianceViewModel(applianceType) {
    var self = this;
    self.Id = ko.observable("-1");
    self.ApplianceType = ko.observable(applianceType);
    self.ServiceApplianceQuantity:"1"
};

viewModel.AddAppliance = function () {
    viewModel.Appliances.push(new ApplianceViewModel(new ApplianceTypeViewModel()));
};

Ultimately it would be nice to have the applianceType copied from the array over the Appliances[i].ApplianceType, but honestly, I’m just trying to get the Id to update there. The rest I can do on the server.

If the data exists, it will change everytime. When I create it new (with AddAppliance), the data will not change.

Thanks Everyone!

  • 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-15T15:44:17+00:00Added an answer on June 15, 2026 at 3:44 pm

    Just drop the “Id” part in the data binding and your Appliances[i].ApplianceType will be correctly filled with the selected appliance type.

    value: $data.ApplianceType.Id should be value: $data.ApplianceType

    Check this jsfiddle for an example of how value binding works http://jsfiddle.net/angelyordanov/gRxMq/.

    And your code modified to work here http://jsfiddle.net/angelyordanov/vvGgD/.

    The way knockout is designed options can be an array of arbitrary javascript objects, not just strings, and the selected item(the value) will be one of those objects. That is why you have the optionsText binding, to specify what is the display text for an option.

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

Sidebar

Related Questions

In the end, I have decided that this isn't a problem that I particularly
I've got a particularly tricky problem using AJAX, which works fine in IE7 and
This is a tricky problem to describe...I have a fixed height <div> on my
It might be easiest to explain the question with an example. I have a
I have recently run into a particularly sticky issue regarding committing the result of
I have a lein project (using cascalog--but that's not particularly important). I'm trying to
I have seen a lot of people in the C++ community(particularly ##c++ on freenode)
I have a question about Spring, particularly the MVC component. I have a jsp
I have a general question about the way that database indexing works, particularly in
I am new to ASP.NET MVC, particularly ajax operations. I have a form with

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.