简体   繁体   English

如何在ASP.NET MVC项目中为knockout生成客户端视图模型?

[英]How can I generate client-side view models for knockout in an ASP.NET MVC project?

I am currently working on an ASP.NET MVC solution and have recently introduced both Knockout (an MVVM JS library) and Wijmo (a set of jQuery UI widgets). 我目前正在开发一个ASP.NET MVC解决方案,最近又推出了Knockout(MVVM JS库)和Wijmo(一组jQuery UI小部件)。

With the introduction of Knockout I also need to have models on the client side, so for this purpose I am serializing the C# ViewModel and attaching it to the view using data-model="@Model.ToJson()" . 随着Knockout的引入,我还需要在客户端有模型,所以为此我将序列化C#ViewModel并使用data-model="@Model.ToJson()"将其附加到视图中。 This allows me to retrieve the model from JS and apply some client-side love to everything. 这允许我从JS检索模型并对一切应用一些客户端的爱。

However, knockout needs everything to be observables, so I need to declare a separate client-side ViewModel and map everything from the data-model object. 但是,knockout需要一切都是可观察的,所以我需要声明一个单独的客户端ViewModel并映射数据模型对象的所有内容。 This feels very much like duplicate effort and I'd like to avoid it somehow. 这感觉非常像重复的努力,我想以某种方式避免它。

I'm hoping someone has a tool or technique to share that will allow me to render the knockout ViewModel directly from the server. 我希望有人有一个共享的工具或技术,这将允许我直接从服务器渲染淘汰ViewModel。 Possible solution could include: 可能的解决方案包括:

  • Custom JSON serialization to render the observable view model directly to the output in the data-model attribute. 自定义JSON序列化,以将可观察视图模型直接呈现给data-model属性中的输出。
  • Automatic client-side transformation (I've heard of ko-autobind, but am not sure if it would be a recommended path to take or how stable/complete it is) 自动客户端转换(我听说过ko-autobind,但我不确定它是否是推荐的路径或者它是如何稳定/完整的)
  • Something I haven't thought of 我没有想到的东西

I'd like the solution to be generic and automatic, as my current approach of typing the observable client-side view models by hand is just too unproductive to be viable. 我希望解决方案是通用的和自动的,因为我目前用手工输入可观察的客户端视图模型的方法实在是太没用了。

How are you solving this problem? 你是怎么解决这个问题的?

According to their tutorials it's just a simple .map function 根据他们的教程,它只是一个简单的.map函数

If this is the ViewModel 如果这是ViewModel

function Task(data) {
    this.title = ko.observable(data.title);
    this.isDone = ko.observable(data.isDone);
}

And this function get's the data from the server, it uses the .map function to inject the server data right into the VM 此函数从服务器获取数据,它使用.map函数将服务器数据直接注入VM

// Data
var self = this;
self.tasks = ko.observableArray([]);

// Load initial state from server, convert it to Task instances, then populate self.tasks
$.getJSON("/tasks", function(allData) {
    var mappedTasks = $.map(allData, function(item) {
        return new Task(item)
    });
    self.tasks(mappedTasks);
});

For ko mapping http://knockoutjs.com/documentation/plugins-mapping.html 对于ko映射http://knockoutjs.com/documentation/plugins-mapping.html

For auto-bind here's an example 对于自动绑定,这是一个例子

https://groups.google.com/forum/#!msg/knockoutjs/IJTx37UXQVw/UTrWdEK1C-oJ https://groups.google.com/forum/#!msg/knockoutjs/IJTx37UXQVw/UTrWdEK1C-oJ

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在ASP.NET Core 1.0中删除Bower并将npm用于客户端? - How can I drop bower and use npm for client-side in ASP.NET Core 1.0? ASP.NET MVC 2客户端验证触发错误 - ASP.NET MVC 2 client-side validation triggering incorrectly 使用MvcContrib FluentHtml的ASP.NET MVC客户端验证 - ASP.NET MVC Client-side validation with MvcContrib FluentHtml ASP.NET MVC客户端验证的自定义属性 - ASP.NET MVC client-side validation of custom attribute 如何在 ASP.NET Core 3.1 MVC 中进行RequiredIf 客户端和服务器端验证? - How to make RequiredIf Client-side and server-side validation in ASP.NET Core 3.1 MVC? 如何使用客户端验证来处理 c# 中的 asp.net mvc? - How to use client-side validation to work on your asp.net mvc in c#? 如何检查ASP.NET MVC 5中客户端是否已存在用户? - How to check if user already exists on client-side in ASP.NET MVC 5? 如何将2个值与ASP.NET MVC中的客户端验证进行比较? - How to compare 2 values with client-side validation in ASP.NET MVC? ASP.NET MVC-使用视图模型时的客户端验证 - ASP.NET MVC - client side validation when using view models 如何使用敲除和ASP.NET MVC将多个视图模型绑定到控制器 - How to bind multiple view models to controller using knockout and asp.net mvc
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM