Send data from MVC to KnockoutJS an show in HTML
ASP.NET
1
Posts
1
Posters
0
Views
1
Watching
-
I have created a list in MVC which looks like this:
<pre lang="text"> var countries = new List<Countries>();countries.Add(new Countries() { Id = 1, Mainland = "a", Name = "a" }); countries.Add(new Countries() { Id = 1, Mainland = "b", Name = "b" }); countries.Add(new Countries() { Id = 1, Mainland = "c", Name = "c" }); countries.Add(new Countries() { Id = 1, Mainland = "d", Name = "d" }); countries.Add(new Countries() { Id = 1, Mainland = "e", Name = "e" }); return View(countries);</pre>
That is my Index page.
Now, I want to retrieve that list in a js script which, for now, looks like this (this works):
<pre lang="text"><script>
var mainViewModel = {
countries: ko.observableArray([]),
initialize: function (model) {
console.log(model)
}
};
mainViewModel.initialize(@Html.Raw(Json.Encode(Model)));
ko.applyBindings(mainViewModel);
</script></pre>But now, I want to show those objects in html and I don't really know how do to that. Can anybody help me?