I am trying to populate knockoutjs viewmodel with some initial values from the server, I am using ASP.Net MVC so the way I am doing it is passing a mvc viewmodel to the view:
public ActionResult Edit(int cvId)
{
CV cv = repository.FindCV(cvId);
//auto mapper mapping
Mapper.CreateMap<CV, MyCVViewModel>();
Mapper.CreateMap<Company, MyCompanyViewModel>();
Mapper.CreateMap<Education, MyEducationViewModel>();
Mapper.CreateMap<Reference, MyReferenceViewModel>();
var model = Mapper.Map<CV, MyCVViewModel>(cv);
return View(model);
}
Inside the view I convert the viewmodel into json string and bind it to knockoutjs viewmodel, so it gets populated with data:
//mvc viewmodel
@model Taw.WebUI.Models.MyCVViewModel
//convert
@{
var json = @Html.Raw(Model.ToJson());
}
//lastly bind
<script type="text/javascript">
// Activate knockout binding
var viewModel = new CVViewModel(@json);
ko.applyBindings(viewModel);
</script>
and inside my knockout javascript, i populate knockout viewmodel with the data:
var CVViewModel = function (data) {
var self = this;
//list view model
self.title = ko.observable(data.title);
self.statement = ko.observable(data.statement);
self.reference = ko.observable(data.reference);
self.companies = ko.observableArray(data.companies);
self.educations = ko.observableArray(data.educations);
self.references = ko.observableArray(data.references);
}
Everything gets populated at this stage:

and the resulting json string is:

Questions:
1. The problem is that some values don't bind when I change them, only title and statement changes:

Resulting json, as you can see, only title and statement changes, and values inside company don't change

2. When saving this data again, how can I let server side know what has been edited and what has been deleted, how to keep track of them using MVC and entity framework, and change database accordingly
Update
My knockout javascript, I already have these observables defined, how do define them in the observablearray
function Company() {
this.companyName = ko.observable();
this.jobTitle = ko.observable();
this.description = ko.observable();
this.startDate = ko.observable();
this.endDate = ko.observable();
}