I am just beginning to learn KnockoutJS so I am probably missing something simple but here is my issue.
Below is my ClassType class
public class ClassType
{
[HiddenInput(DisplayValue = false)]
public int ID { get; set; }
[Required(ErrorMessage = "Please enter a description")]
public string Description { get; set; }
[Display(Name = "Class Slots")]
public int ClassSlots { get; set; }
}
I have a method in my controller to return a list of all the types set up called GetAll
public JsonResult GetAll()
{
var classtypes = classtypeRepository.All;
return Json(classtypes, JsonRequestBehavior.AllowGet);
}
In my view I have the following code to load the data into my viewmodel
<script type="text/javascript">
$(document).ready(function () {
$.get("/ClassTypes/GetAll/", "", function (data) {
var viewModel = {
classtypes: ko.observableArray()
};
viewModel.classtypes = ko.mapping.fromJS(data);
ko.applyBindings(viewModel);
});
});
</script>
I have thrown the following div into my view to make sure something is returned
<div data-bind="text: ko.toJSON($data)"></div>
The following is returned
{"classtypes":[{"ID":1,"Description":"Online","ClassSlots":30},{"ID":2,"Description":"In-House","ClassSlots":15},{"ID":3,"Description":"Hands-On","ClassSlots":10}]}
If I try to bind anything to the view model it does not seem to understand the properties
<tbody>
<!-- ko.foreach: classtypes -->
<tr>
<td></td>
<td data-bind="text: Description">
</td>
<td data-bind="text: ClassSlots">
</td>
</tr>
<!-- /ko -->
</tbody>
I used the knockoutJS mapping plugin to bring the data in which I thought would create the structure of the viewmodel for me. What am I missing?
<td data-bind="text: $data.Description">
- Pavlo