Knockoutjs和绑定

时间:2013-07-01 10:41:41

标签: asp.net-mvc knockout.js knockout-2.0

我对knockuoutjs和绑定感到疯狂:

我已经定义了一个CreateEditGroup.js文档,我创建了方法和Collection来检索或更新我的应用程序中的一个组:

var url = window.location.pathname;
var GroupID = url.substring(url.lastIndexOf('/') + 1);


var Group = function (group)
{
    var self = this;
    self.GroupID = ko.observable(group ? group.GroupID : 0).extend({ required: true });
    self.Name = ko.observable(group ? group.Name : '').extend({ required: true });
};

var GroupCollection = function () {
    var self = this;
    if (GroupID == 0) {
        self.group = ko.observable(new Group());
    }
    else {
        $.ajax({
            url: '/Group/GetGroupByID/' + GroupID,
            async: false,
            dataType: 'json',
            success: function (json) {
                self.group = ko.observable(new Group(json));
            }
        });
    }

    self.backToGroupList = function () { window.location.href = '/App/Groups' };

    //Aggiunta o modifica

    self.saveGroup = function () {
        $.ajax({
            type: (self.group().GroupID > 0 ? 'PUT' : 'POST'),
            cache: false,
            dataType: 'json',
            url: urlContact + (self.group().GroupID > 0 ? '/UpdateGroup?id=' + self.group().GroupID : '/SaveGroup'),
            data: JSON.stringify(ko.toJS(self.group())),
            contentType: 'application/json; charset=utf-8',
            async: false,
            success: function (data) {
                window.location.href = '/App/Groups';
            },
            error: function (err) {
                var err = JSON.parse(err.responseText);
                var errors = "";
                for (var key in err) {
                    if (err.hasOwnProperty(key)) {
                        errors += key.replace("group.", "") + " : " + err[key];
                    }
                }
                $("<div></div>").html(errors).dialog({ modal: true, title: JSON.parse(err.responseText).Message, buttons: { "Ok": function () { $(this).dialog("close"); } } }).show();
            },
            complete: function () {
            }
        });
    };

};

ko.applyBindings(new GroupCollection());

显示表单的视图包含以下HTML代码:

@{
    ViewBag.Title = "CreateEditGroup";
}

<h2>Nuovo gruppo</h2>
<table class="table">
    <tr>
        <th colspan="1">

        </th>
    </tr>
    <tr></tr>
    <tbody data-bind="with: Group">
        <tr>
            <td>
                <input class="input-large" data-bind="value: Name" placeholder="Nome" />
            </td>
        </tr>
    </tbody>
</table>

<button class="btn btn-small btn-success" data-bind="click: saveGroup">Salva</button>
<input class="btn btn-small btn-primary" type="button" value="Annulla" data-bind="click: $root.backToGroupList" />

<script src="@Url.Content("~/Repository/CreateEditGroup.js")"></script>

每次加载CreateEditGroup页面时,我都会收到绑定Name属性的消息,但代码看起来不错。

请帮帮我。

代码错误:

An unhandled exception occurred at line 1936 column 17 in http://localhost:2297/Scripts/knockout-2.2.1.debug.js

0x800a139e - Run-time JavaScript: Unable to parse bindings.

Message: ReferenceError: 'Name' is not defined;

Bindings value: value: Name

2 个答案:

答案 0 :(得分:0)

我相信你有大写错误。

data-bind="with : Group"

应该是

data-bind="with : group"

答案 1 :(得分:0)

我已经解决了这个难题! 错误很简单:在CreateEditGroup.js中,我声明了一个名为Group的变量和一个名为group的对象

var Group = function (group)
{
    var self = this;
    self.GroupID = ko.observable(gruppo ? gruppo.GroupID : 0).extend({ required: true });
    self.Name = ko.observable(gruppo ? gruppo.Name : '').extend({ required: true });
};

我已经使用其他名称修改了此函数中传递的对象的名称,最终可以使用!

var Group = function (gruppo)
{
    var self = this;
    self.GroupID = ko.observable(gruppo ? gruppo.GroupID : 0).extend({ required: true });
    self.Name = ko.observable(gruppo ? gruppo.Name : '').extend({ required: true });
};

谢谢大家的帮助!