我正在使用淘汰赛 MVC。在我的页面中,我渲染了几个局部视图。在每个局部视图中,我传递一个模型,即主模型的子模型。而且我收到一个错误“未捕获的错误:您不能将绑定多次应用于同一个元素。” 这是一些代码。
主视图
@using PerpetuumSoft.Knockout
@model UserPageViewModel
@{
var ko = Html.CreateKnockoutContext();
}
<div>
@Html.Partial("Controls/_PagePanel", Model.PanelViewModel)
</div>
@ko.Apply(Model)
页面面板局部视图
@using PerpetuumSoft.Knockout
@model PagePanelViewModel
@{
var ko = Html.CreateKnockoutContext();
}
<div>
<div>
@foreach (var button in Model.Actions)
{
@ko.Html.Button(button.Id, button.ActionName, button.ControllerName, null, button.HtmlAttributes)
}
<a id="searchGridButton" class="btn" data-toggle="collapse" data-parent="#searchPanel" href="#accordionfilterContainer">Advanced search</a>
</div>
@Html.Partial("Controls/_AdvancedSearch", Model.AdvancedSearch)
</div>
@ko.Apply(Model)
高级搜索部分视图
@using PerpetuumSoft.Knockout
@model AdvancedSearchViewModel
@{
var ko = Html.CreateKnockoutContext();
}
<div id="accordionfilterContainer" class="accordion-body collapse">
@using (ko.Html.Form(Model.ActionName, Model.ControllerName, null, new { id = "searchForm" }))
{
<div id="filterContainer" class="accordion-inner">
@using (var items = ko.Foreach(m => m.SearchCriteria))
{
<div style="display: inline-block">
<span @items.Bind.Text(m => m.Id)></span>
<br />
@items.Html.TextBox(m => m.Value, null)
</div>
}
<div style="display: inline-block">
<button type="submit" class = "@Model.HtmlAttributes">Apply filters</button>
</div>
</div>
}
</div>
@ko.Apply(Model)
模型
public class UserPageViewModel
{
public List<User> Data { get; set; }
public PagePanelViewModel PanelViewModel { get; set; }
}
public class PagePanelViewModel
{
public IEnumerable<ButtonViewModel> FilterButtons { get; set; }
public AdvancedSearchViewModel AdvancedSearch { get; set; }
public IEnumerable<ButtonViewModel> Actions { get; set; }
}
public class AdvancedSearchViewModel
{
public IList<TextBoxViewModel> SearchCriteria { get; set; }
}
我明白,问题出在@ko.Apply() 字符串中,但不明白为什么。有没有办法解决这个问题?