Knockout Binding选择更改MVC 5

时间:2016-03-09 14:21:38

标签: jquery model-view-controller knockout.js knockout-3.2

我在我的MVC应用程序模型上使用Knockout的“foreach”函数来加载下拉选项表。当所选下拉值更改时,我需要跟踪模型中的更改。

我尝试在模型中的元素上使用subscribe选项,但是当它更改时,我绑定到它的函数不会触发。我知道有几种方法可以实现这种改变,但是我更喜欢坚持使用这种定义我的函数的格式,如视图底部所示。

型号:

    public class ProfileLookAndFeelViewModel : StandardLayoutViewModel
        {
            public ProfileLookAndFeelViewModel()
            {
                Form = new FormGroup();
                PriceUOMDropdownOptions = new PriceUOM();
                UOMInformation = new UOMInformationGroup();
            }

            public FormGroup Form { get; set; }
            public PriceUOM PriceUOMDropdownOptions { get; set; }
            public UOMInformationGroup UOMInformation { get; set; }

            public class FormGroup
            {            
                public bool FormValueChanged { get; set; }            
            }

            public class PriceUOM
            {
                public int Id { get; set; }
                public String Name { get; set; }
                public string Code { get; set; }
            }

            public class PriceUOMOverride
            {
                public string ItemDescription { get; set; }
                public string TemplateCode { get; set; }
                public string UOMDesc { get; set; }
                public List<PriceUOM> PriceUOMDropdownOptions { get; set; }
                public int SelectedPriceUOM { get; set; }
                public int SelectedPriceUOMOriginal { get; set; }
                public bool SelectedPriceUOMChanged { get; set; }
            }  

            public class UOMInformationGroup
            {
                public UOMInformationGroup()
                {
                    UOMs = new List<PriceUOMOverride>();
                }

                public List<PriceUOMOverride> UOMs { get; set; }
                public bool UOMsChanged { get; set; }
            }
        }

查看:

    <table class="table table-bordered table-hover table-striped">
        <thead>
            <tr>
                <th>Description</th>
                <th>Template Code</th>
                <th>Default UOM</th>
            </tr>
        </thead>
        <tbody>
            <!-- ko foreach: $root.UOMInformation.UOMs -->
            <tr>
                <td data-bind="text: ItemDescription"></td>
                <td data-bind="text: TemplateCode"></td>
                <td class="center" >
                    <select data-bind="options: PriceUOMDropdownOptions, optionsText: 'Code', optionsValue: 'Id', value: SelectedPriceUOM, css: { important: SelectedPriceUOMChanged() == true }"></select>
                </td>
            </tr>
            <!-- /ko -->
        </tbody>
    </table>  

    <script type="text/javascript">
    $(function () {
        var vmProfileLookAndFeel = function () { var self = this; };
        vmProfileLookAndFeel = ko.mapping.childrenIndependently($.parseJSON('@Html.RawJsonForKoMapping(Model)'), ["UOMInformation", "Form", "PriceUOMDropdownOptions"]);
        vmProfileLookAndFeel.UOMInformation.UOMs.subscribe(function (newValue) {
            vmProfileLookAndFeel.ValueChanged();
        });

        vmProfileLookAndFeel.ValueChanged = function () {
            //Do something;
        };

        ko.applyBindings(vmProfileLookAndFeel);
        });
    </script>

我尝试了这个建议,但这里的答案不是从MVC模型加载数据而是加载硬编码数组

IDEONE demo

修改

我更新了视图以反映将数组添加为observable。

这是我调用以映射对象的函数

    ko.mapping.childrenIndependently = function (jsObject, childrenArray) {
        var mapping = {
            "ignore": childrenArray
        }
        var vm = ko.mapping.fromJS(jsObject, mapping);

        // handle children
        for (var childrenArrayIndex = 0; childrenArrayIndex < childrenArray.length; childrenArrayIndex++) {
            if (jsObject.hasOwnProperty(childrenArray[childrenArrayIndex])) {
                // if the property is an array, create an objservable array
                // and map each child
                // else map the property
                if ($.isArray(jsObject[childrenArray[childrenArrayIndex]])) {
                    vm[childrenArray[childrenArrayIndex]] = ko.observableArray();

                    for (var childObjectArrayIndex = 0; childObjectArrayIndex < jsObject[childrenArray[childrenArrayIndex]].length; childObjectArrayIndex++) {
                        vm[childrenArray[childrenArrayIndex]].push(ko.mapping.childrenIndependently(jsObject[childrenArray[childrenArrayIndex]][childObjectArrayIndex], childrenArray.slice(childrenArrayIndex)));
                    }
                }
                else {
                    vm[childrenArray[childrenArrayIndex]] = ko.mapping.childrenIndependently(jsObject[childrenArray[childrenArrayIndex]], childrenArray.slice(childrenArrayIndex));
                }
            }
        }

        return vm;
    };

1 个答案:

答案 0 :(得分:0)

对于这种情况,我最终添加了一个隐藏在<select>元素之后的计算函数,每次添加一行时都会调用该函数。从那里我可以操纵生成的对象。

    <td class="center" >
        <select data-bind="options: PriceUOMDropdownOptions, optionsText: 'Code', optionsValue: 'Id', value: SelectedPriceUOM, css: { important: SelectedPriceUOMChanged() == true }"></select>
        <span class="hide" data-bind="text: $root.OnTheFlyCalculations($data)()"></span>
    </td>

新功能

    vmProfileLookAndFeel.OnTheFlyCalculations = function (item) {
        return ko.computed({
            read: function () {                 
                return item.SomethingChanged(true);
            }
        }, vmProfileLookAndFeel)
    };