我在 asp.net 中使用淘汰赛。我希望重用绑定到视图模型的分部视图。即两种用法,在每种情况下绑定到不同的视图模型。
<div class="col-xs-5" data-bind="with: vm1">
@Html.Partial("~/Views/Shared/Customers/Modals.cshtml")
@Html.Partial("~/Areas/Shared/Views/Lookup.cshtml")
</div>
<div class="col-xs-5" data-bind="with: vm2">
@Html.Partial("~/Views/Shared/Customers/Modals.cshtml")
@Html.Partial("~/Areas/Shared/Views/Lookup.cshtml")
</div>
这适用于显示基于正确视图模型的查找。
但是,在视图模型代码中,搜索来自模态的模型div 标签,然后显示模态。这在其中一个地方导致问题,因为找到的"模态div"总是相同的(我认为是第一个(。
有没有办法搜索一个div标签,表明我想要相对于我所在位置的东西?
来自后端开发人员:)的 BR
您可以根据
视图模型中的id
属性(id 或其他任何内容(为每个div
添加唯一的 id
属性:
<div class="col-xs-5" data-bind="with: vm1, attr:{id: vm1.id}">
@Html.Partial("~/Views/Shared/Customers/Modals.cshtml")
@Html.Partial("~/Areas/Shared/Views/Lookup.cshtml")
</div>
<div class="col-xs-5" data-bind="with: vm2, attr:{id: vm2.id}">
@Html.Partial("~/Views/Shared/Customers/Modals.cshtml")
@Html.Partial("~/Areas/Shared/Views/Lookup.cshtml")
</div>
然后在您的视图模型中:
var ViewModel = function(data) {
var self = this;
// Generate a random string, you might want to use your own method there
this.id = ko.observable( Math.random().toString(36).substring(7) );
// Div selection:
self.findDiv = function() {
// or $('#'+self.id()) if using JQuery
return document.getElementById(self.id());
}
}