javascript - Knockout.js 选择值绑定(bind)到对象

标签 javascript asp.net-mvc-3 select binding knockout.js

当使用对象作为选择列表值时,我无法使用 Knockout 选择列表绑定(bind)。如果我使用字符串,它工作正常,但我想绑定(bind)对象。

我有一个 Gift 对象,它有标题、价格和公司。我有一个精选的公司列表,每个公司都有一个 ID 和名称。然而,初始选择在选择列表中并不正确。

请参阅 fiddle :http://jsfiddle.net/mrfunnel/SaepM/

绑定(bind)到 MVC3 View 模型时,这对我很重要。虽然我承认这可能是因为我做事的方式不对。

如果我有以下模型:

public class Company
{
    public Guid Id { get; set; }
    public string Name { get; set; }

}
public class GiftModel
{
    public Company Company { get; set; }
    public string Title { get; set; }
    public double Price { get; set; }
}

如何选择可在我的 Controller 中绑定(bind)的公司?我是否需要将 CompanyId 属性添加到 GiftModel 并绑定(bind)到它或编写自定义 Binder 。我是否遗漏了一些基本的东西?

提前致谢。

最佳答案

你需要做很多事情。

ViewModel 中的 CompanyId 是绑定(bind)并使其可观察的唯一方法。 你不能让一个对象只有它的值才可观察

<form class="giftListEditor" data-bind="submit: save">
    <!-- Our other UI elements, including the table and ‘add’ button, go here -->

    <p>You have asked for <span data-bind="text: gifts().length">&nbsp;</span> gift(s)</p>
    <table>
        <tbody  data-bind="foreach: gifts">
            <tr>
                <td>Gift name: <input data-bind="value: Title"/></td>
                <td>Price: $ <input data-bind="value: Price"/></td>
                <td>Company: <select data-bind="options: $root.companies, optionsText: 'Name', optionsValue: 'Id', value: CompanyId"/></td>
                <td>CompanyId: <span data-bind="text: CompanyId"></span></td>
                <td><a href="#" data-bind="click: $root.removeGift">Delete</a></td>
            </tr>
        </tbody>
    </table>
    <button data-bind="click: addGift">Add Gift</button>
    <button data-bind="enable: gifts().length > 0" type="submit">Submit</button>
</form>​

你的模型

// Fake data
var initialData = [
    { Title: ko.observable('Item 1'), Price: ko.observable(56), CompanyId: ko.observable(1) },
    { Title: ko.observable('Item 2'), Price: ko.observable(60), CompanyId: ko.observable(2) }, 
    { Title: ko.observable('Item 3'), Price: ko.observable(70), CompanyId: ko.observable(2) }
];

var initialCompanies = [
    { Id: 1, Name: "Comp 1" },
    { Id: 2, Name: "Comp 2" },
    { Id: 3, Name: "Comp 3" }
];

var viewModel = {
    gifts: ko.observableArray(initialData),
    companies: initialCompanies,

    addGift: function() {
        this.gifts.push({
            Title: "",
            Price: "",
            Company: { Id: "", Name: "" }
        });
    },
    removeGift: function($gift) {
        viewModel.gifts.remove($gift);
    },
    save: function() {
        console.log(ko.toJS(viewModel.gifts));
    }
};

ko.applyBindings(viewModel, document.body);​

关于javascript - Knockout.js 选择值绑定(bind)到对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9839248/

相关文章:

asp.net-mvc - 如何使用 Razor 创建显示选择的单选列表框

php - 如何向按名称排序的 foreach 循环选择框添加选项?

javascript - 无法访问同一类内的变量

javascript - 根据浏览器宽度通过平滑过渡更改网格中的列数

javascript - 通过http传输大量json

c# - 如何将 json 对象传递给 mvc Controller

c# - 将字符串(带有 UTC)转换为日期时间

javascript - 它不验证我的提交按钮点击使用 javascript

sql - DB2 SELECT EXCEPT 与 WHERE 子句

php - where条件个数不固定时的SELECT查询?