Ajax 使用敲除绑定json数据

Ajax 使用敲除绑定json数据,ajax,json,asp.net-mvc-3,knockout.js,knockout-mapping-plugin,Ajax,Json,Asp.net Mvc 3,Knockout.js,Knockout Mapping Plugin,我有一个“假定”与json结果绑定的表: <table> <thead> <tr> <th> Id </th> <th> Number </th> <th> Name </th> <th>

我有一个“假定”与json结果绑定的表:

<table>
<thead>
    <tr>
        <th>
            Id
        </th>
        <th>
            Number
        </th>
        <th>
            Name
        </th>
        <th>
            Password
        </th>
        <th>
            Role
        </th>
    </tr>
</thead>
<tbody data-bind="foreach: Employees">
    <tr>
        <td>
            <span data-bind="text: EmployeeId"></span>
        </td>
        <td>
            <span data-bind="text: EmployeeNumber"></span>
        </td>
        <td>
            <span data-bind="text: EmployeeName"></span>
        </td>
        <td>
            <span data-bind="text: EmployeePassword"></span>
        </td>
        <td>
            <span data-bind="text: EmployeeRole"></span>
        </td>
    </tr>
</tbody>
更新:以下是我返回的json数据示例:

 {"Employees":[{"EmployeeId":1,"EmployeeName":X","EmployeeNumber":"1","EmployeePassword":"x","EmployeeRole":"User"},{"EmployeeId":10,"EmployeeName":"S","EmployeeNumber":"21","EmployeePassword":"s","EmployeeRole":"Admin"}]}

假设服务器端一切正常,您的模型将绑定到.model属性,但您的foreach应该是

<tbody data-bind="foreach: model.Employees">

这就是我现在所能看到的,需要看到更多的代码来进一步帮助

您可能希望使用firebug或chrome开发工具,确认收到的javascript错误(如果有),同时检查响应以确保您的操作方法返回数据

编辑

好的,这是您的代码的一部分。一些东西,可能是打字错误,但以防万一。标记中缺少标记,并且在第一个EmployeeName之前的json中有一个未终止的注释

您确实需要使用上述模型。员工。因为您是从mvc返回json,它将具有正确的头,所以jquery将自动将其转换为javascript对象,因此不需要使用fromJSON,而是尝试fromJS

希望这有帮助。


<script id="tmpl_ScreenNavigation" type="text/html">
<tr class="erow">
    {{if ScreenParentNevigationId}}
    <td width="250px"><table cellpadding="5" class="Permission" sceenid="${ScreenNevigationId}" border="0" RoleDetailId="0"><tr><td><label><input type="checkbox" id="view${ScreenNevigationId}" class="View" />&nbsp;View</label></td><td><label><input type="checkbox" id="AddModify${ScreenNevigationId}" class="Save"/>&nbsp;Add/Modify</label></td><td><label><input type="checkbox" id="Delete${ScreenNevigationId}" class="Delete" />&nbsp;Delete</label></td></tr></table></td>
    {{/if}}
    {{if !ScreenParentNevigationId}}
    <td {{if !ScreenParentNevigationId}} style="background-color: #F9BDC8;"{{/if}} width="250px"><table cellpadding="5" class="Permission" sceenid="${ScreenNevigationId}" border="0" RoleDetailId="0"><tr><td {{if !ScreenParentNevigationId}} style="background-color: #F9BDC8;"{{/if}}><label><input type="checkbox" id="view${ScreenNevigationId}" class="View" />&nbsp;View</label></td></tr></table></td>
    {{/if}}
    <td {{if !ScreenParentNevigationId}} style="background-color: #F9BDC8;"{{/if}}>${ScreenName}</td>
    <td {{if !ScreenParentNevigationId}} style="background-color: #F9BDC8;"{{/if}}>${ScreenDescription}</td>
</tr>
</script>
{{if ScreenParentNevigationId} 查看添加/修改删除 {{/if} {{if!ScreenParentNevigationId} 看法 {{/if} ${ScreenName} ${ScreenDescription}

$(“#tmpl_屏幕导航”).tmpl(resultJson).appendTo(“#tblRoles”)

将JavaScript标记的位置更改为html文件顶部表格上方。因此,数据可能无法正确填充。

ko.applyBindings(viewModel.Model);另外@madcapnmckay关于运行开发人员控制台的建议也非常好。敲除的绑定错误消息很有用。或者viewModel=ko.mapping.fromJSON(数据);我到底应该编辑什么?我没有得到你的评论…如果你没有在浏览器的开发控制台中得到错误,F12在许多浏览器上,不确定。看来你上面所说的是不可能的,我希望会有错误。我认为,我对你的代码所做的两项修改中的任何一项都会绑定。没有更多信息,就无法提供帮助。您在控制台中看到了什么错误?没有错误。。。您可以在上面我的更新中看到返回的json数据。。。我相信我的脚本就是问题所在……创建javascript模板,然后将json数据绑定到表:$(“#tmpl_ScreenNavigation”).tmpl(resultJson).appendTo(“#tblRoles”)
<tbody data-bind="foreach: model.Employees">
<script id="tmpl_ScreenNavigation" type="text/html">
<tr class="erow">
    {{if ScreenParentNevigationId}}
    <td width="250px"><table cellpadding="5" class="Permission" sceenid="${ScreenNevigationId}" border="0" RoleDetailId="0"><tr><td><label><input type="checkbox" id="view${ScreenNevigationId}" class="View" />&nbsp;View</label></td><td><label><input type="checkbox" id="AddModify${ScreenNevigationId}" class="Save"/>&nbsp;Add/Modify</label></td><td><label><input type="checkbox" id="Delete${ScreenNevigationId}" class="Delete" />&nbsp;Delete</label></td></tr></table></td>
    {{/if}}
    {{if !ScreenParentNevigationId}}
    <td {{if !ScreenParentNevigationId}} style="background-color: #F9BDC8;"{{/if}} width="250px"><table cellpadding="5" class="Permission" sceenid="${ScreenNevigationId}" border="0" RoleDetailId="0"><tr><td {{if !ScreenParentNevigationId}} style="background-color: #F9BDC8;"{{/if}}><label><input type="checkbox" id="view${ScreenNevigationId}" class="View" />&nbsp;View</label></td></tr></table></td>
    {{/if}}
    <td {{if !ScreenParentNevigationId}} style="background-color: #F9BDC8;"{{/if}}>${ScreenName}</td>
    <td {{if !ScreenParentNevigationId}} style="background-color: #F9BDC8;"{{/if}}>${ScreenDescription}</td>
</tr>
</script>