0

我正在使用淘汰赛 MVVM 创建剑道组合框。我期望的是选择选定的对象作为这个例子(chosenTicket 包含对象,而不是属性)。这是我的代码(使用 asp.net razor)。C#代码:

@{
    var accountExample = new List<BankAccountSetupViewModel>()
    {
        new BankAccountSetupViewModel { ID = 1, Bank = "ANZ Panin Bank", AccountName = "Primary", AccountNumber = "1234-567-890" },
        new BankAccountSetupViewModel { ID = 2, Bank = "ABN Amro Group", AccountName = "Backup", AccountNumber = "2345-678-901" },
        new BankAccountSetupViewModel { ID = 3, Bank = "Bank BRI", AccountName = "Cadangan", AccountNumber = "3456-789-012" },
        new BankAccountSetupViewModel { ID = 4, Bank = "Bank Central Asia Jakarta", AccountName = "Primary", AccountNumber = "4567-890-123" },
        new BankAccountSetupViewModel { ID = 5, Bank = "BNI", AccountName = "Backup", AccountNumber = "5678-901-234" }
    };
}

HTML 代码:

<fieldset id="form-container" data-bind="with: formEditorSettlement">
    <legend>Edit Detail</legend>
    @Html.LabelFor(model => model.Amount)
    <span style="margin-bottom: 20px;">
        @Html.TextBoxFor(model => model.Currency.Code, new { @readonly = true, data_bind = "value: currency", @style = "width: 50px;" })
        @Html.TextBoxFor(model => model.Amount, new { data_bind = "value: amount" })
    </span>
    <div class="cols-2">
        <label class="cols-full">Bank Account</label><br />
        <label>Bank</label>
        <span>@(Html.Kendo().ComboBox().Name("BankAccount").Suggest(true).Filter(FilterType.Contains).DataTextField("Name").DataValueField("ID").BindTo(accountExample).HtmlAttributes(new { @class = "no-input", style = "width: 300px;", data_bind = "kendoComboBox: { data: $root.accounts, value: $root.chosenBankAccount, dataTextField: 'Bank', dataValueField: 'ID' }" }))</span>
        <label>Account No.</label>
        <span>@Html.TextBoxFor(model => model.BankAccount, new { @readonly = true, data_bind = "value: $root.chosenBankAccount.AccountName" })</span>
        <label>Account Name</label>
        <span>@Html.TextBox("BankAccountName", null, new { @readonly = true, data_bind = "value: bankAccountName" })</span>
    </div>
</fieldset>

Javascript代码:

(function ($, ko, undefined) {
    var viewModel = namespace('ViewModels.Activities');
    viewModel.SettlementSplitObservable = new (function (defaultSettlementSplit, defaultAccounts) {
        var root = this;
        //add isChecked to all item
        ko.utils.arrayForEach(defaultSettlementSplit, function (item) {
            item.isChecked = ko.observable(false);
        });
        root.verifiedItems = ko.observableArray(defaultSettlementSplit);
        root.lastAction = ko.observable('add');
        root.maxAmount = ko.observable(@(Model.Splitted[0].Amount));
        root.accounts = ko.mapping.fromJS(defaultAccounts);
        root.chosenBankAccount = ko.observable();
        root.formEditorSettlement = new (function () {
            var self = this;
            self.currency = ko.observable('@Model.Currency.Code');
            self.amount = ko.observable(0);
            self.bankNameBank = ko.observable('');
            self.bankAccountNo = ko.observable('');
            self.bankAccountName = ko.observable('');
            self.isEditMode = ko.observable(false);
        }.bind(this));
    })(@Html.Raw(Json.Encode(Model.Splitted)),
       @Html.Raw(Json.Encode(accountExample)));
})(jQuery, ko)

我所期待的是,当用户在 ComboBox“BankAccount”中选择项目时,选择的BankAccount 应该包含来自“BankAccountSetupViewModel”的对象(从 C# 代码生成,使用敲除映射插件映射)。任何方法将不胜感激。

4

1 回答 1

2

Kendo 组合框不支持选择整个对象。您可以指定 adataValueField将值设置为对象的唯一键 (id)。然后,您可以创建一个ko.computed来表示所选对象。喜欢:

this.choices = ko.observableArray([
    { id: "1", name: "apple"},
    { id: "2", name: "orange"},
    { id: "3", name: "banana"}
]);

this.selectedId = ko.observable();
this.selectedChoice = ko.computed(function() {
    var id = this.selectedId();
    if (id) {
        return ko.utils.arrayFirst(this.choices(), function(choice) {
           return choice.id === id; 
        });
    }  
}, this);

示例:http: //jsfiddle.net/rniemeyer/zbEQC/

于 2013-03-05T14:46:52.390 回答