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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T08:48:00+00:00 2026-06-17T08:48:00+00:00

I found this, but this doesn’t work for me: Bind to simple array of

  • 0

I found this, but this doesn’t work for me: Bind to simple array of strings

I am just trying to manage an array of strings through knockout. Here is my markup:

<div class="editor-field">
        <table>
            <thead>
                <tr>
                    <th>Page Urls</th>
                    <th></th>
                </tr>
            </thead>
            <!-- ko if: RouteUrls().length > 0 -->
            <tbody data-bind="foreach: RouteUrls">                   
                <tr>
                    <td><span data-bind="text: $data"></span></td>
                    <td><a href="#" class="ui-icon ui-icon-closethick" data-bind="click: $root.removeUrl">Remove</a></td>
                </tr>
            </tbody>
            <!-- /ko -->
            <!-- ko if: RouteUrls().length < 1 -->
            <tbody>
                <tr>    
                    <td colspan="3"> No urls added for editing.</td>
                </tr>
            </tbody>
            <!-- /ko -->
        </table>
        @Html.HiddenFor(x => x.UrlCount, new { data_bind = "value:RouteUrls().length" })
        @Html.ValidationMessageFor(x => x.UrlCount)
    </div>

And here is my knockout code:

    <script type="text/javascript">
    var mappedModel = ko.mapping.fromJS(@Html.Raw(JsonConvert.SerializeObject(Model)));
    mappedModel.RouteUrls = ko.observableArray([]);
    mappedModel.urlToAdd = ko.observable("/");
    mappedModel.addUrl = function() {
        var duplicateValues = ko.utils.arrayFilter(this.RouteUrls(), function (item) {
                return item == mappedModel.urlToAdd();                   
        });

        if ((this.urlToAdd() != "") && (duplicateValues.length == 0)) // Prevent blanks and duplicates
        {

            this.RouteUrls().push(this.urlToAdd());
            this.urlToAdd("/");
            console.log(this.RouteUrls().length);
            console.log(this.RouteUrls());
        }
    };
    mappedModel.removeUrl = function(url) {
        mappedModel.RouteUrls.remove(url);
    };

    //other code...

    ko.applyBindings(mappedModel);

    $.validator.unobtrusive.parse("#pageCForm");
    $("#pageForm").data("validator").settings.submitHandler = mappedModel.save;
</script>

Now the problem is, in my markup, the collection never shows. When I try and add a url, even if I remove the knockout if conditional and just display the table, it is always empty. If I try and submit the page, I have unobtrusive validation running on the hidden field for URL count and it acts like it’s empty too. So everything in the markup is acting like RouteUrls() is always empty. But when I log the RouteUrls().length and text to the console I get the following after successive attempts to add:

1 
["/sdfsd"] 
2 
["/sdfsd", "/trgfd"] 
3 
["/sdfsd", "/trgfd", "/fdfdggdbjn"] 

So I don’t understand how in the script code the length and array are properly getting populated, but it’s not being reflected in the markup. Any 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-17T08:48:01+00:00Added an answer on June 17, 2026 at 8:48 am

    The problem is (probably) on this line :

    this.RouteUrls().push(this.urlToAdd());
    

    Knockout redefined the push (and every other methods that change an array) in the ObservableArray. These ObservableArray methods will change the array and then trigger the observers.

    So you should replace it with :

    this.RouteUrls.push(this.urlToAdd());
    

    More infos here :
    http://knockoutjs.com/documentation/observableArrays.html#manipulating_an_observablearray

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

Sidebar

Related Questions

I've found this topic for PostData using http. but it doesn't work. What's wrong?
First of all, I found this: Objective C HTML escape/unescape , but it doesn't
Ok, so I'm quite new to jQuery, but found this bizzare problem just now,
I'm trying to find this algorithm on C++ in .NET but can't, I found
I found this jquery plugin which does exactly what I need, but I want
I found this question but it was only similar and, more importantly, dated by
I found this sample but its for web. Can any one check this proj.
I found this similar question here , but this is really old. Was it
like in this post here by exemple I have found this blog but in
I've tried all the time calculating examples I found on this site but somehow

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.