我在我的 MVC 应用程序模型上使用 Knockout 的“foreach”函数来加载下拉选项表。When the selected dropdown value changes, I need to track that change in the model.
我尝试在模型中的元素上使用 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 模型加载数据,而是从硬编码数组加载
编辑:
我更新了视图以反映将数组添加为可观察的。
这是我调用来映射对象的函数
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;
};