我的抽象超类TogglePanelViewModel具有5个可覆盖的属性 4是虚拟的,因为它具有默认值设置和一个摘要。我想用这个摘要 局部视图中的viewmodel,然后由继承TogglePanelViewModel的子类呈现。
我的问题是:-
这是代码
抽象视图模型:
public abstract class TogglePanelViewModel
{
public abstract string LSName { get; }
public virtual string LabelText { get; set; } = "Hide welcome text|Show welcome text";
public virtual string StyleClass { get; set; } = "";
public virtual string StylePullRight { get; set; } = "pull-right";
public virtual bool RenderLabelAfterToggle { get; set; } = false;
}
使用抽象视图模型的共享局部视图:
@model App.Website.Areas.Default.ViewModels.TogglePanelViewModel
<div class="@Model.StyleClass toggle-filter @Model.StylePullRight" id="ls" name="@Model.LSName">
@if (Model.RenderLabelAfterToggle == false)
{
<div class="toggle-label">
<span>@Model.LabelText</span>
</div>
}
<div class="common-slide-checkbox @Model.StylePullRight">
<input type="checkbox" value="1" id="toggle-filter-visible">
<label for="toggle-filter-visible" id="toggle-pos"></label>
<div id="toggle-bg"></div>
</div>
@if (Model.RenderLabelAfterToggle == true)
{
<div class="toggle-label">
<span>@Model.LabelText</span>
</div>
}
</div>
Ex:继承TogglePanelViewModel的基类的1
会给出错误
The model item passed into the dictionary is of type 'App.Website.Areas.Create.Models.IdeationViewModel', but this dictionary requires a model item of type 'App.Website.Areas.Default.ViewModels.TogglePanelViewModel'.
也无法创建实例新的TogglePanelViewModel {}
查看:
@using App.Website.Areas.Default.ViewModels
@model App.Website.Areas.Create.Models.IdeationViewModel
<div>
@Html.Partial("~/Views/Shared/TogglePanel.cshtml", Model.ToggleIdeationFilter)
</div>
Viewodel:
public class IdeationViewModel
{
...
public ToggleIdeationFilterVM ToggleIdeationFilter { get; set; }
}
public class ToggleIdeationFilterVM : TogglePanelViewModel
{
public override string LSName => null;
public override string LabelText => "Hide filter|Show filter";
}
谢谢
答案 0 :(得分:0)
答案:将ToggleIdeationFilterVM的实例作为模型传递给部分视图:-
@Html.Partial("~/Areas/Default/Views/Shared/TogglePanel.cshtml", new ToggleIdeationFilterVM())