【问题标题】:replacing DisplayTemplate with knockout binding用敲除绑定替换 DisplayTemplate
【发布时间】:2013-03-12 19:00:31
【问题描述】:

我目前正在使用 DisplayTemplate 来显示我的模型的属性:

@Html.DisplayFor( m => m.TheProperty, "myTemplate")

模板输出图片的html,来源取决于模型(TheProperty)是否为空。

现在,我将视图中的所有绑定替换为淘汰赛绑定。所以看来我不能再使用 DisplayTemplates 了,可以吗?由于现在控件已绑定到 js ViewModel,我将如何将模板传递给模型?

【问题讨论】:

  • 服务器端 MVC 和客户端 MVVM 是两个完全不同的东西,您必须使用 JSON 服务将所有模型移动到客户端。
  • 是的,我已经这样做了。我正在使用淘汰赛将 MVC 模型映射到 MVVM 模型。现在我的问题是用其他等价物替换我的保持 DisplayTemplates 的最佳方法是什么?

标签: binding asp.net-mvc-4 knockout.js display-templates


【解决方案1】:

KO 中的等价物是模板绑定,但它的语法不是很好,但它就像

<div data-bind="template: { name: 'display-for-my-property', data: myProperty }"></div>

其中 display-for-my-property 是带有 myProperty 模板的脚本标签的 ID

你可以看到这个语法很混乱,我已经建立了自己的库来解决这个问题

https://github.com/AndersMalmgren/Knockout.BindingConventions

使用我的 lib,同样的场景看起来像

<div data-name="myProperty"></div>

示例 http://jsfiddle.net/xJL7u/

你可以扩展我的库,这样你就可以了

<div data-display="myProperty"></div>

<div data-editor="myProperty"></div>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-27
    • 2016-03-21
    • 2015-05-26
    • 1970-01-01
    • 1970-01-01
    • 2011-08-13
    相关资源
    最近更新 更多