【发布时间】:2015-07-18 01:04:11
【问题描述】:
我的 MVC 模型中有如下对象列表。
@Html.EditorFor(model => model.LstEmploymentHistory)
<button id="btnAddHistory" data-bind="click: addHistory">Add one</button>
我需要在加载时将其绑定到 Knockout 可观察数组。
<script type="text/javascript">
//Data
var History = function () {
var self = this;
self.CompanyName = ko.observable();
self.Designation = ko.observable();
self.StartDate = ko.observable();
self.EndDate = ko.observable()
};
var viewModelEmploymentHistory = function () {
// Operations
var self = this;
self.historyList = ko.observableArray([new History()]); // Put one line in by default
self.addHistory = function () { self.historyList.push(new History()) }
self.removeHistory = function (history) {
self.historyList.remove(history)
}
self.educationList = ko.observableArray([new Education()]); // Put one line in by default
self.addEducation = function () { self.educationList.push(new Education()) };
self.removeEducation = function (education) {
self.educationList.remove(education)
}
};
//Data
var Education = function () {
var self = this;
self.Degree = ko.observable();
self.YearOfPassing = ko.observable();
self.Percentage = ko.observable();
self.Institute = ko.observable()
};
//var masterVM = (function () {
// this.viewModelEducationalDetails = new viewModelEducationalDetails(),
// this.viewModelEmploymentHistory = new viewModelEmploymentHistory();
//})();
//ko.applyBindings(masterVM)
ko.applyBindings(new viewModelEmploymentHistory());
// ko.applyBindings(new viewModelEducationalDetails(), document.getElementById("containerEducation"));
//ko.applyBindings(new viewModelEmploymentHistory(), document.getElementById("containerHistory"));
</script>
任何人都可以帮助我在页面加载时将 C# 列表绑定到可观察数组吗?
【问题讨论】:
-
这与 C# 有什么关系?
-
上次我检查 C# 是否是有效的 MVC 语言? @Jamiec
-
@Liam - 让我换个说法;这和 MVC 有什么关系?
-
感谢利亚姆的评论。我已经实现了动态添加 div 的功能。我可以添加 div。在页面加载时,默认情况下 div 将是以下代码的一个 bcoz self.addHistory = function () { self.historyList.push(new History()) } 我现在需要一个更新或显示 div 的功能。在加载时我将获得项目列表,我需要使用淘汰赛绑定它们。请指导我如何实现这一目标或最好的方法是什么。?
标签: c# arrays knockout.js asp.net-mvc-5