无法使用knockoutjs处理MVC重定向中的绑定

时间:2015-02-27 05:02:34

标签: javascript jquery asp.net asp.net-mvc knockout.js

我正在研究knockoutjs并遇到一些问题。

我有ASP.NET MVC页面,在我面临以下问题的新页面上进行重定向。

  

未捕获的ReferenceError:无法处理绑定“with:function   (){return students}“消息:无法处理绑定”值:   function(){return FirstName}“消息:未定义FirstName

我的代码:

<html>
<head>
<script src="~/Scripts/jquery-1.9.1.js"></script>
<script src="~/Scripts/knockout-3.2.0.js"></script>
</head>
<body>
<div>

<input type="button" class="btn btn-small btn-primary" data-bind="click: createProfile"  value="New Contact"/>
<table class="table table-striped table-bordered table-condensed">
<tr> 
<th>LastName Name</th>
<th>FirstName Name</th>
<th>EnrollmentDate</th>
</tr>
<tbody data-bind="foreach: students">
<tr>
<td data-bind="text: StudentID,click: editProfile"></td>
<td data-bind="text: LastName"></td>
<td data-bind="text: FirstName"></td>
<td data-bind="text: EnrollmentDate"></td>

<td><button class="btn btn-mini btn-danger" data-bind="click: removeProfile">remove</button></td>
</tr>
</tbody>
</table>
</div>
</body>
</html>


<script>
students = new Object();
var self = this;

var applied = false;

$(document).ready(function ()
{
self.createProfile = function () {
window.location.href = '/Contact/CreateEdit/0';
};

self.editProfile = function (students) {
window.location.href = '/Contact/CreateEdit/' + students.StudentID;
};

self.removeProfile = function (students) {
if (confirm("Are you sure you want to delete this profile?")) {
self.Profiles.remove(profile);
}
};

var StudentsViewModel = function () {
var refresh = function () {
$.getJSON("/Contact/GetAllStudents", function (data) {
self.students(data);
ko.applyBindings(students);
})
};
self.students = ko.observableArray([]);
refresh();
};
ko.applyBindings(new StudentsViewModel());
applied = true;

});


</script> 

0 个答案:

没有答案