I am trying to use KnockoutJS and MVC 4 to display a table with ActionLink definitions in the first column of a table. The display of the data itself is extremely straightforward, and I have no problems. The problem is generating ActionLink .
I examined Using MVC helpers inside jquery.tmpl templates , but the solution does not use knockout templates there and inserting Url into the model object is impossible (application domain model objects used to create the presentation model will be widely used through the application).
Table definition:
<table> <tbody data-bind="template: { name: 'dmuTableDetail', foreach: tables() }"></tbody> </table>
( tables is an observable array, hence parens).
Knockout pattern definition:
<script id="dmuTableDetail" type="text/html"> <tr> <td>@Html.ActionLink("Details", "Details", "DMUTableCategory", new { @Id = ??? } )</td> <td data-bind="text:TableId"></td> <td data-bind="text:TableName"></td> </tr> </script>β
View Model Definition:
var PageViewModel = function () { self = this; self.tables = ko.observableArray([]); self.readItems = function () { self.tables(jQuery.parseJSON('[{"TableId":1001, "TableName":"Table#1"},{"TableId":1002, "TableName":"Table#2"}]')); } } $(document).ready(function () { vm = new PageViewModel(); self.readItems(''); ko.applyBindings(vm); });
(the actual code makes an Ajax call to retrieve the data, but the code above also demonstrates the problem).
No matter what I replace ??? , I canβt get the value of the TableId field that I need to insert in href.
Any help would be greatly appreciated.
Thankyou.