Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/80.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# KnockOutJS绑定问题_C#_Jquery_Asp.net Mvc 4_Knockout.js - Fatal编程技术网

C# KnockOutJS绑定问题

C# KnockOutJS绑定问题,c#,jquery,asp.net-mvc-4,knockout.js,C#,Jquery,Asp.net Mvc 4,Knockout.js,我使用的是MVC4、jquery2.03和knockout2.30,我无法看到全部依赖项。 我是不是遗漏了什么 模型 看法 @model IEnumerable @{ ViewBag.Title=“Index”; } 指数 总数:受抚养人 var initialData=@Html.Raw(Json.Encode(Model)); var viewModel={ 依赖项:ko.observatarray(初始数据) }; $(document.ready)(函数(){ko.applyBindin

我使用的是MVC4、jquery2.03和knockout2.30,我无法看到全部依赖项。 我是不是遗漏了什么

模型

看法

@model IEnumerable
@{
ViewBag.Title=“Index”;
}
指数
总数:受抚养人

var initialData=@Html.Raw(Json.Encode(Model)); var viewModel={ 依赖项:ko.observatarray(初始数据) }; $(document.ready)(函数(){ko.applyBindings(viewModel);});
从这个JSFIDLE中可以看到,您的敲除绑定很好:

这意味着问题与您的初始数据有关。在脚本中使用Razor语法很可能就是问题所在。您可以向控制器添加一个方法,该方法以字符串形式返回数据,然后使用
$.getJSON()
调用检索数据,然后填充视图模型(我们的代码就是这样操作的)

对于您的控制器,您可以有如下内容:

[HttpGet]
public string GetDependents()
{
    Dependent[] dependents = GetAllDependents();
    return new JavaScriptSerializer().Serialize(dependents);
}
$.getJSON("http://mysite.com/controller/GetDependents", function(data) {  
     $.each(data, function(key, val) {
         viewModel.dependent.push(val);
     });
});
然后在Javascript中,类似于:

[HttpGet]
public string GetDependents()
{
    Dependent[] dependents = GetAllDependents();
    return new JavaScriptSerializer().Serialize(dependents);
}
$.getJSON("http://mysite.com/controller/GetDependents", function(data) {  
     $.each(data, function(key, val) {
         viewModel.dependent.push(val);
     });
});

(这是我的全部想法,所以可能注释准确无误,但应该能让您接近)。

在我的控制器中添加了以下内容

    [HttpGet]
    public string GetDependents()
    {
        var dependents = _db.Dependents;
        return new JavaScriptSerializer().Serialize(dependents);
    }
看法

@model IEnumerable
@{
ViewBag.Title=“Index”;
}
指数
总数:受抚养人

$.getJSON(“http://localhost:1233/Dependent/GetDependents,函数(数据){ $。每个(数据、函数(键、值){ viewModel.dependent.push(val); }); }); $(document.ready)(函数(){ko.applyBindings(viewModel);});
当我打电话时 我得到以下回应 [{“RelationshipManager”:{},“Id”:1,“姓名”:“高帽子”,“年龄”:10},{“RelationshipManager”:{},“Id”:2,“姓名”:“长斗篷”,“年龄”:12}]


但它仍然没有显示计数

不确定出了什么问题,但混合使用服务器端渲染(Razor)和JavaScript是不好的。请检查initialData。很可能它不是一个数组,或者是一个空数组;var viewModel={dependent:ko.observatarray(initialData)}$ready(函数(){ko.applyBindings(viewModel);});在document.ready中添加:
警报(initialData.length)并查看它给您带来了什么。
@model IEnumerable<eManager.Domain.Dependent>

@{
    ViewBag.Title = "Index";
}

<h2>Index</h2>

<p>Total: <span data-bind="text: dependent().length">&nbsp;</span> dependent(s)</p>


<script type="text/javascript">
    $.getJSON("http://localhost:1233/Dependent/GetDependents", function (data) {
        $.each(data, function (key, val) {
            viewModel.dependent.push(val);
        });
    });

    $(document).ready(function () { ko.applyBindings(viewModel); });
</script>