0

I'm currently using a DisplayTemplate to show a property of my Model:

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

The template output the html for an image, which source depends on whether the Model (TheProperty) is null or not.

Now, I'm replacing all bindings in my View with knockout binding. So it seems I can no longer use DisplayTemplates, can I ? How would I pass the template a Model since now controls are bound to a js ViewModel ?

Sam
  • 13,934
  • 26
  • 108
  • 194
  • Server side MVC and client side MVVM are two totally separate things, you have to move all your models to the client using a JSON service. – Anders Mar 13 '13 at 09:32
  • yes, I've done so. I'm using knockout to map MVC model to MVVM model. Now my problem what is the best way to replace my hold DisplayTemplates with something else equivalent ? – Sam Mar 13 '13 at 09:35

1 Answers1

0

The equivalent in KO would be the template binding, its syntax is not as nice however, its like

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

Where display-for-my-property is the id of a script tag with the template for myProperty

As you can see this synstax is pretty messy, I have made my own library that solves this

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

Using my lib the same scenario would look like

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

Example http://jsfiddle.net/xJL7u/

You could extend my lib so you can do

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

And

<div data-editor="myProperty"></div>
Anders
  • 17,306
  • 10
  • 76
  • 144