My application is MVC 5 c#, I am using the following form to update a table:
@using (Ajax.BeginForm("AddOha", "Medication", new AjaxOptions()
{
UpdateTargetId = "result",
OnSuccess = "getresult",
HttpMethod = "POST"
}))
{
<input id="MedName" name="MedName" value="" class="input-sm form-control" style="width: 100px" placeholder="Value"/>
<input type="submit" value="Save" class="btn btn-primary"/>
}
Here is my Knockout list:
<tbody data-bind="foreach: OhaList">
<tr>
<td>
<input data-bind="value: Name" class="input-sm form-control" style="width: 100px" placeholder="Value" />
</td>
</tr>
</tbody>
Here is the ViewModel:
var ViewModel = function() {
var self = this;
this.OhaList = ko.observableArray([]),
$.ajax({
type: "GET",
url: '@Url.Action("xxx", "xxx")',
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (data) {
self.OhaList(data);
},
error: function (err) {
alert(err.status + " : " + err.statusText);
}
});
var ohaMed = {
Name: self.Name
};
self.ohaMed = ko.observable();
}
ko.applyBindings(new ViewModel());
I tried to update the list using:
var getresult = function(data) {
if (data[0].Name !== "") {
// alert(data[0].Name);
ViewModel.OhaList.push({ Name: data[0].Name });
// ViewModel.OhaList.push({ Name: "1});
} else {
alert("failed");
}}
Although I get the results from the controller, or even if I code the value, I get the following error:
Unable to get property 'push' of undefined or null reference
Would appreciate your suggestions.