javascript - Knockoutjs 使用 MVC 从服务器填充数据

标签 javascript c# json asp.net-mvc knockout.js

我正在尝试使用服务器中的一些初始值填充knockoutjs View 模型,我使用的是ASP.Net MVC,所以我这样做的方式是将mvc View 模型传递给 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);
}

在 View 内,我将 viewmodel 转换为 json 字符串并将其绑定(bind)到 knockoutjs viewmodel,因此它会填充数据:

//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>

在我的 knockout JavaScript 中,我使用数据填充 knockout View 模型:

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);
}

在这个阶段一切都会被填充:

enter image description here

生成的 json 字符串是:

enter image description here

问题:

<强>1。问题是,当我更改某些值时,它们不会绑定(bind),只有标题和语句更改:

enter image description here

生成的 json,如您所见,只有标题和语句发生变化,公司内部的值没有变化 enter image description here

<强>2。当再次保存这些数据时,如何让服务器端知道哪些内容已编辑、哪些内容已删除,如何使用 MVC 和 Entity Framework 跟踪它们,并相应地更改数据库

更新

我的knockout javascript,我已经定义了这些可观察量,如何在可观察数组中定义它们

function Company() {
    this.companyName = ko.observable();
    this.jobTitle = ko.observable();
    this.description = ko.observable();
    this.startDate = ko.observable();
    this.endDate = ko.observable();
}

最佳答案

对于你的第一个问题:

问题是您需要对每个数组项使用ko.observable

例如:jsfiddle

function CVViewModel(data) {
    var self = this;

    //list view model
    self.title = ko.observable(data.title);
    self.companies = ko.observableArray(data.companies.map(Company));
}

function Company(data) {
    if (!(this instanceof Company)){
        return new Company(data);
    }
    this.companyName = ko.observable(data.companyName || '');
    this.jobTitle = ko.observable(data.jobTitle || '');
    this.description = ko.observable(data.description || '');
    this.startDate = ko.observable(new Date(data.startDate) || '');
    this.endDate = ko.observable(new Date(data.endDate) || '');
}

现在,当您将公司可观察量绑定(bind)到 UI 时, View 模型上的每个数组元素都将保持同步。

关于你的第二个问题,我建议使用像 breeze.js 这样的 ORM它为您进行更改跟踪。 Breeze.js 有一个 tutorial使用knockout.js。

关于javascript - Knockoutjs 使用 MVC 从服务器填充数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28732641/

相关文章:

javascript - js正则字符集包括 ""或 ""

c# - 单例查找表需要锁吗?

c# - 互斥 : is this safe?

python - ODS 文件转 JSON

javascript - 这种计算 Canvas 元素中非白色像素的方法有什么问题?

javascript - 使用history.js 面临后退按钮单击事件的问题

c# - 将 MYSQL 日期获取到 C# 字符串异常中

javascript - $.getJSON() + $.each() 导致某些对象的键名称未定义

c# - 控制 joopz,可能是 jquery 或 php

javascript - github主题功能如何在javascript中实现