Das verwirrt mich. Es muss etwas Kleines sein, das ich nicht sehe. Ich versuche, mit einem Ajax-Aufruf eine sehr einfache observableArray
im Knockout zu laden.
Javascript
// we bind the array to the view model property with an empty array.
var data = [];
var viewModel = {
vendors: ko.observableArray(data)
};
ko.applyBindings(viewModel);
$(function () {
// on this click event, we popular the observable array
$('#load').click(function () {
// WORKS. Html is updated appropriately.
viewModel.vendors([{ "Id": "01" },{ "Id": "02" },{ "Id": "03" }]);
// DOES NOT WORK. Fiddler2 shows the same exact json string come back
// as in the example above, and the success function is being called.
$.ajax({
url: '/vendors/10',
dataType: 'json',
success: function (data) {
viewModel.vendors(data);
}
});
});
});
html
<button id="load">Load</button>
<ul data-bind="template: { foreach: vendors }">
<li><span data-bind="text: Id"></span></li>
</ul>
Frage: Warum wird beim erfolgreichen Aufruf von ajax der Wert der data
-Variablen von Byte zu Byte mit dem hart eingegebenen Wert übereinstimmt und nicht die HTML-Aktualisierung ausgelöst?
Es gibt keinen Grund, warum dies nicht funktionieren würde. Wie das demonstriert.
http://jsfiddle.net/madcapnmckay/EYueU/
Ich würde überprüfen, ob der Ajax-Post tatsächlich Json-Daten zurückgibt und dass Json ein Array ist und dass es richtig analysiert wird.
Ich musste den Ajax-Aufruf anpassen, damit die Geige-Ajax-Handler korrekt arbeiten.
Mir fällt nichts mehr ein.
Hoffe das hilft.
var self=this;
//var self first line in model
$.ajax({
url: ",
dataType: "json",
contentType: 'application/json',
type: "POST",
data: JSON.stringify({ }),
processdata: true,
beforeSend: function () {
$.mobile.loading('show');
},
error: function (xhr, textStatus, errorThrown) {
alert('Sorry!');
},
success: function (data) {
$.mobile.loading('hide');
if (data.result!= '') {
self.vendors(data.result);
} else {
self.vendors({something});
}
}
});
Verwenden Sie self.vendors, nicht dieses viewModel.vendors
Folgendes habe ich in meiner MVC .net-App mit Knockout und Jquery gemacht.
// Scripts/groItems.js
(function () {
var ViewModel = function () {
items = ko.observableArray(),
ItemName = ko.observable(),
Img = ko.observable(),
Qty = ko.observable()
}
$.getJSON('/Items2/AllItems', function (data) {
for (var i = 0; i < data.length; i++) {
self.items.Push(data[i]);
}
});
var vm = new ViewModel();
$(function () {
ko.applyBindings(vm);
});
}());
@model IEnumerable<GroModel.Item>
@{
ViewBag.Title = "Index";
}
<p>
@Html.ActionLink("Create New", "Create")
</p>
<div data-bind="text: items().length"></div>
<table class="container table table-hover">
<thead>
<tr>
<th>Item name</th>
<th>img</th>
<th>qty</th>
</tr>
</thead>
<tbody data-bind="foreach: items">
<tr>
<td data-bind="text: ItemName"></td>
<td data-bind="text: Img"></td>
<td data-bind="text: Qty"></td>
</tr>
</tbody>
</table>
@section Scripts {
<script src="~/Scripts/knockout-3.4.2.js"></script>
<script src="~/Scripts/groItems.js"></script>
}
Folgendes ist ein Teil meines Codes bei Items2Controller.cs
private GroContext db = new GroContext();
public JsonResult AllItems()
{
return Json(db.Items.ToList(), JsonRequestBehavior.AllowGet);
}
Hoffe das wird helfen. Vielen Dank