Javascript Knockout.js使用push方法添加数据不更新视图
我从WebApi获取JSON格式的数据,然后使用KnockoutJS的Javascript Knockout.js使用push方法添加数据不更新视图,javascript,jquery,asp.net-mvc,json,knockout.js,Javascript,Jquery,Asp.net Mvc,Json,Knockout.js,我从WebApi获取JSON格式的数据,然后使用KnockoutJS的.push()方法将接收到的数据添加到MVC视图中。我在POST响应中收到的JSON数据是正确的,因此我认为客户端出现了问题-而不是我获取的数据未定义和[Object]。 尽管页面刷新后所有数据都显示正确。 这里是我的淘汰码: <script> var viewModel = { prepp: ko.observableArray(), currentPage: ko.obs
.push()
方法将接收到的数据添加到MVC视图中。我在POST响应中收到的JSON数据是正确的,因此我认为客户端出现了问题-而不是我获取的数据未定义和[Object]
。
尽管页面刷新后所有数据都显示正确。
这里是我的淘汰码:
<script>
var viewModel = {
prepp: ko.observableArray(),
currentPage: ko.observable(-1)
};
$(function () {
getData(viewModel.currentPage() + 1);
ko.applyBindings(viewModel, document.getElementById("prepps"));
});
//This function used for paging, not concern to question directly
function getData(pageNumber) {
if (viewModel.currentPage() != pageNumber) {
$.ajax({
url: "/api/index",
type: "get",
contentType: "application/json",
data: { id: pageNumber }
}).done(function (data) {
if (data.length > 0) {
viewModel.currentPage(viewModel.currentPage() + 1);
for (var i = 0; i < data.length; i++) {
viewModel.prepp.push(data[i]);
}
}
});
};
//Here we call POST action of WebApi.
$(".send-feed").click(function () {
var guid = getguid();
var styles;
var req = { RequestName: $("#request").val(), RequestDescription: $("#request-review").val(), RequestOwner: $("#username").val(), RequestGuid: guid, RequestStyles: [] }
$("div.click").each(function () {
styles = { RequestGuid: guid, StyleId: $(this).text() };
req.RequestStyles.push(styles);
});
var model = JSON.stringify(req);
$.ajax({
url: "/api/index",
type: "POST",
contentType: "application/json, charset: utf-8",
data: model
}).done(function (data) {
viewModel.prepp.push(data);
});
});
}
});
</script>
var viewModel={
prepp:ko.observearray(),
当前页面:可观察到的ko(-1)
};
$(函数(){
getData(viewModel.currentPage()+1);
应用绑定(viewModel,document.getElementById(“prepps”);
});
//此函数用于分页,与直接提问无关
函数getData(页码){
if(viewModel.currentPage()!=页码){
$.ajax({
url:“/api/index”,
键入:“获取”,
contentType:“应用程序/json”,
数据:{id:pageNumber}
}).完成(功能(数据){
如果(data.length>0){
viewModel.currentPage(viewModel.currentPage()+1);
对于(变量i=0;i
下面是MVC视图标记:
div class="prepp-blocks-container" data-bind="foreach: prepp" id="prepps">
<div class="prepp-block">
<div class="star" data-bind="if: $data.IsStylistOffer == true">
<img src="../../img/star-yellow.png" alt="added by stylist">
</div>
<a data-bind="attr: {href: 'Request/' + $data.RequestGuid}"><h3 data-bind="text: $data.RequestName"></h3></a>
<span data-bind="foreach: {data: RequestStyles, as: 'style'}">
<div data-bind="text: style" class="taste-prepp"></div>
</span>
<p class="text-small-grey" data-bind="text: $data.PreppsNumber + ' prepps'"></p>
</div>
</div>
div class=“prepp blocks container”data bind=“foreach:prepp”id=“prepps”>
我认为应该调整控制器的返回类型,使其与视图模型结构相匹配,如
public model Get()
{
//build your list .
return model ;
}
尝试使用ko.mapping.toJS()
,这样就不会失去淘汰赛的优势
参考敲除文档,您可以找到更多相关信息,我们如何更好地使用它好的,那么您的意思是说当您单击“.send feed”时,视图在您将新值推入viewmodel.prepp时不会更新,对吗?这应该是这样的if:$data.IsStylistOffer()==true
缺少大括号。@kundansinghchoushan,不完全是。添加了新项,但没有绑定值,而是未定义和[Object]。@supercool,可能我会添加。所以,如果我没说错的话,您没有从控制器获取数据,对吗?如果是这样,只需将控制器代码与返回类型共享