将项目动态添加到HTML列表

时间:2016-02-21 14:56:47

标签: javascript c# jquery asp.net knockout.js

我目前正在使用knockout在asp.net MVC 5应用程序中构建一个向导。在其中一个步骤中,我需要将项添加到html列表中。也就是说,文本进入文本框,然后单击添加的按钮,文本将添加到列表中。

我的模板:

<script type="text/html" id="addProduct">
<li class="dd-item bordered-inverse animated fadeInDown">
    <div class="dd-handle dd-nodrag">
        <div class="checkbox">
            <label>
                <a data-bind="attr: { 'href': '#' }" class='btn btn-xs icon-only success'><i class="fa fa-trash-o"></i></a>
                <label data-bind="text: Name, uniqueName: true"></label>
            </label>
        </div>
    </div>
</li>
</script>

<div class="row">
  <div class="col-md-6">
    <div id="newProduct" class="dd">
        <ol class="dd-list" data-bind="template: { name: 'addProduct', foreach: Model.Step1.ProductServices }"></ol>
    </div>
   </div>
</div>

输入文本框:

@Html.TextBoxFor(model => model.Step1.ProductService, new { data_bind = "value: Model.Step1.ProductService")

<button type="button" id="addservice" data-bind="event:{ click: AddProduct }">Add Service/Product</button>

淘汰赛添加赛事:

self.AddProduct = function () {
     self.Model.CurrentStep().ProductServices.push({ name: self.Model.CurrentStep().ProductService });
}

self.Model.CurrentStep()。ProductServices是一个列表产品,只是一个属性名称。

上面的代码将项添加到html列表并更新UI,但self.Model.CurrentStep()。ProductServices.length始终为零,这意味着没有任何内容添加到模型本身。

请问如何强制它更新self.Model.CurrentStep()。ProductServices?

1 个答案:

答案 0 :(得分:1)

length不是observableArray的属性。请改为ProductServices().length

相关问题