How do I get Knockout to see my Model object

27 views
Skip to first unread message

pbai...@gmail.com

unread,
Apr 21, 2014, 11:20:06 PM4/21/14
to knock...@googlegroups.com
At the top of my partial view I have "@model PeopleDTO", which is a List<PeopleDTO> object (in C#)  I would like to have Knockout display the list of names as a link, so I can click on each name.  I'm fairly new to javascript and knockout, but not new to C#.  All the examples in the Knockout site seem to deal with data arrays embedded in the javascript.  Probably not real world, at least not mine. :-)   so I'm needing to transition from accessing in-line arrays to database models.

I tried the following code, but this didn't work:

var personList = '@(Model)';

function PersonViewModel() {
        var self = this;

        self.People = ko.observable(personList);
        self.Name = ko.computed(function() {
            return self.firstName() + " " + this.lastName();
        });
    }

    ko.applyBindings(new PersonViewModel);

Any help would be appreciated.

Gunnar Liljas

unread,
Apr 22, 2014, 2:16:37 AM4/22/14
to knock...@googlegroups.com
You have to render the data in a format that JS can understand.

@Html.Raw(Json.Encode(Model))


--
You received this message because you are subscribed to the Google Groups "KnockoutJS" group.
To unsubscribe from this group and stop receiving emails from it, send an email to knockoutjs+...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages