I have list of objects in my MVC model as below.
@Html.EditorFor(model => model.LstEmploymentHistory)
<button id="btnAddHistory" data-bind="click: addHistory">Add one</button>
I need to bind this to Knockout observable array on load.
<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>
Can any one please help me with the way to bind the C# list to the observable array on page load.?