[英]Dynamically adding items to HTML list
我目前正在使用淘汰賽在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?
length
不是observableArray
的屬性。 而是檢查ProductServices().length
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.