简体   繁体   中英

Binding a generic list with knockout.js in MVC5

I am using knockout.js to display the amount of articles I have in my shopping cart. The shopping cart information are saved in a List of the Model ShoppingCartItem. Because I did not read about a way to bind a list with knockout.js directly I am pushing my list items into an array and bind this array afterwards.

But no matter what I am trying the output is always "You have Articles in your shopping cart.". So the length of my array is not displayed.

I added knockout.js in the bundle config file:

bundles.Add(new ScriptBundle("~/bundles/knockout").Include(
                        "~/Scripts/knockout-3.3.0.js"));

            bundles.Add(new ScriptBundle("~/bundles/knockout").Include(
                        "~/Scripts/knockout-3.3.0.debug.js"));

            bundles.Add(new ScriptBundle("~/bundles/knockout").Include(
                        "~/Scripts/knockout.validation.js"));

            bundles.Add(new ScriptBundle("~/bundles/knockout").Include(
                        "~/Scripts/knockout.validation.debug.js"));

My view Model ShoppingCartItem:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace OnlineShop.Models
{
    public class ShoppingCartItem
    {
        public Products product { get; set; }
        public int amount { get; set; }
    }
}

My partial view where I want to use model binding with knockout.js:

@model List<OnlineShop.Models.ShoppingCartItem>

<script type="text/javascript">
    var myArray = [];

    @foreach (var d in Model)
    {
        @:myArray.push("@d");

    }

    var viewModel = {
        cartItems: ko.observableArray(myArray)
    };

    ko.applyBindings(cartItems, document.getElementById("test"));
</script>

<div id="top-cart" class="clearfix">
    <div class="shoppingcart-txt">
        @if (Model != null && Model.Count() > 0)
        {
            double sum = 0.0F;

            for (int i = 0; i < Model.Count(); i++)
            {
                sum = sum + (Model[i].amount * Model[i].product.PurchasePrice);
            }

            <p>
                You have <span id = "test" data-bind="text: cartItems().length">&nbsp;</span> articles in your shopping cart. <br /> 
                <strong>
                    Sum: @sum.ToString("#,##0.00") EUR <br />
                    excl. 3.00 EUR shipping costs
                </strong> 
            </p>
        }
        else
        {
            <p>
                You have no articles in your shopping cart.



            </p>
        }
    </div>
    <div class="shoppingcart-img">
        <a href="@Url.Action("ShoppingCart", "Home")">
            <img src="~/Photos/shoppingcart.png" alt="" border="0" />
        </a>
    </div>
</div>

I also tried to apply the bindings with the following:

ko.applyBindings(cartItems, document.body);

With the same result. The length of my array is not displayed.

I would really appreciate if anyone can tell me what I am doing wrong.

Change your binding to: ko.applyBindings(viewModel, document.getElementById("top-cart"));

And move your script below the HTML. The way it is, is runnig before the HTML element is created.

http://jsbin.com/sazupasope/edit?html,js,output

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM