我认为我有以下内容:
receiptInstance.healthServices.collect().each {
//I've tried to use receiptInstance.healthServices.clear()
//but Set remains with all items
//delete item from Set
receiptInstance.healthServices.remove(it)
//delete item from DB
it.delete()
}
//save with flush to commit the delete
if (!receiptInstance.save(flush: true)) {
render(view: "edit", model: [receiptInstance: receiptInstance])
return
}
item.Attribute info是C#视图模型的一部分。现在我用KO视图模型替换它,并且只有AttributeInfo。 那么,我如何使用KO替换指定的视图部分?
答案 0 :(得分:0)
像这样的东西应该有效......
<div class="attributes" data-bind="html: attributeInfo"></div>
<!-- assume KO is already included -->
<script>
function ViewModel(attributeInfo){
this.attributeInfo = ko.observable(attributeInfo || '');
};
ko.applyBindings(new ViewModel('@Html.Raw(item.AttributeInfo)'));
</script>
现在HTML仍然需要以某种方式输出到页面(因为KO运行客户端,而模型,就目前而言,是服务器端)。我也没有对item.AttributeInfo
执行任何包含会导致JS错误的内容(例如值中的'
)的安全措施。
但是,您可以将其清理干净(以及使用KO映射)并使其更清洁:
@{
IHtmlString modelJs = new HtmlString(
JsonConvert.SerializeObject(
item,
Formatting.None,
new CamelCasePropertyNamesContractResolver()
)
);
}
<div class="attribute" data-bind="html:attributeInfo"></div>
<script>
var viewModel = ko.mapping.fromJS(@modelJs);
ko.applyBindings(viewModel);
</script>