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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T04:09:13+00:00 2026-06-04T04:09:13+00:00

My View is bound to a Model and displays several dropdown lists, and their

  • 0

My View is bound to a Model and displays several dropdown lists, and their number and the options are based on some properties in the model.

I would like to use knockout js to data bind each selected value of the dropdown lists to an observable array, keeping track of which value belongs to which ddl.

Each dropdown list is displayed using the following code:

@for (var i = 0; i < Model.ProductProfiles.Count; i++)
        {
            var productProfile = Model.ProductProfiles[i];
            if (productProfile.IsVarying)
            {
                var lastItem = 0;           
                <div class="tab-pane" id="@productProfile.Name">                        
                    @for (var j = 0; j < productProfile.Attributes.Count; j++)
                    {           

                        <text>@productProfile.Attributes[j].Name</text>                                                     
                        @Html.DropDownListFor(m => m.ProductProfiles[i].SelectedValues[j], (SelectList)ViewData["Levels_" + i + "_" + j],
                        new { @class = "ddl-levels" })
                        <br />
                    }
                </div>
            }
        }

The dropdownlists are correctly filled in and the default value is correctly set (using both the model and the select list passed in the ViewData).

Is there a way to databind to an array that looks something like:

array[0][“0”, “1”, “3”, “1”,….] where array[0] refers to Model.ProductProfiles[0] and the second dimension of the array holds the selected values for the ddl as displayed in the for loop
?

I tried setting my view model as simple as

var viewModel = {
    selectedValues: ko.observableArray([])              
};

and out of desperation I tried to modify the data-bind value to simulate a positional access in the observable array:

@Html.DropDownListFor(m => m.ProductProfiles[i].SelectedValues[j], (SelectList)ViewData["Levels_" + i + "_" + j],
                        new { @class = "ddl-levels", data_bind = "value: productProfiles([" + i + "][" + j + "])" })

Unfortunately it didn’t work…

I hope to have been clear, otherwise I would be glad to clarify when asked to.
Is there anyone who can help me?

Thanks

EDIT: based on comment advice, I split the multidimensional array into 3 observable arrays
like the following:

var viewModel = {        
    selectedValuesProd0: ko.observableArray(),
    selectedValuesProd1: ko.observableArray(),
    selectedValuesProd2: ko.observableArray()
}

and then I modified the HTML code

var observableArrayProduct = "selectedValuesProd0";
                    if (i == 1)
                    {
                        observableArrayProduct = "selectedValuesProd1";
                    }
                    else if (i == 2)
                    {
                        observableArrayProduct = "selectedValuesProd2";
                    }

@Html.DropDownListFor(m => m.ProductProfiles[i].SelectedValues[j], (SelectList)ViewData["Levels_" + i + "_" + j], new { @class = "ddl-levels", data_bind = "value: " + observableArrayProduct + "()[" + j+ "]" })

As you can see I’m still trying a positional access to the mono-dimensional observable array, but it’s still not keeping track of the selected values when the user changes something in the DropDown lists.

At this point I’m starting to think that positional access doesn’t work, but if that’s so, what else can I do?

EDIT 2: SOLVED!

Ok, I was missing a key step: each item in the observable array is itself an observable value, so I fixed it like the following:

var viewModel = {        
    selectedValuesProd0: ko.observableArray(ko.utils.arrayMap(selectedValuesPerProduct[0], function(item) {
        return ko.observable(item);
    })),
    selectedValuesProd1: ko.observableArray(ko.utils.arrayMap(selectedValuesPerProduct[1], function(item) {
        return ko.observable(item);
    })),
    selectedValuesProd2: ko.observableArray(ko.utils.arrayMap(selectedValuesPerProduct[2], function(item) {
        return ko.observable(item);
    }))
}

Where selectedValuesPerProduct is a bidimensional array built using the model passed from the server side code to the View (after serialization).

I hope this can help!

  • 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-04T04:09:15+00:00Added an answer on June 4, 2026 at 4:09 am

    I realised I had never closed this question, so I’m adding a response here:

    Ok, I was missing a key step: each item in the observable array is itself an observable value, so I fixed it like the following:

    var viewModel = {        
    selectedValuesProd0: ko.observableArray(ko.utils.arrayMap(selectedValuesPerProduct[0], function(item) {
        return ko.observable(item);
    })),
    selectedValuesProd1: ko.observableArray(ko.utils.arrayMap(selectedValuesPerProduct[1], function(item) {
        return ko.observable(item);
    })),
    selectedValuesProd2: ko.observableArray(ko.utils.arrayMap(selectedValuesPerProduct[2], function(item) {
        return ko.observable(item);
    }))
    

    }

    Where selectedValuesPerProduct is a bidimensional array built using the model passed from the server side code to the View (after serialization).

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

Sidebar

Related Questions

I have a view in MVC3 with a TextBoxFor bound to my model like
I have Listbox which is bound to my view model. This view model has
I have a register model thats tighly bound to a register view. The register
I have a checkbox bound to an observable on a view model. I have
I have a HTML element bound to one of observables in my view model
I've got a custom control that gets bound to an object from view model.
I created a view and viewmodel that I would like to use twice (or
I have a DataGrid bound to a PagedCollectionview property of my View-Model. I've added
My view model implements INotifyPropertyChanged for properties that it makes available to my view.
I have a ComboBox whose properties ItemsSource and SelectedValue are bound to a model.

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.