我有以下关系的三种类型Search
,BaseSearch
和TextSearch
。 Search
由List<BaseSearch>
和TextSearch
继承自BaseSearch
组成,BaseSearch
是抽象的。
现在我想使用Html帮助器绘制TextSearch
列表,EditorTemplate在下面
@model GenericSearch.Models.TextSearch
<table class="form-group container">
<tr class="row">
<td class=" col-sm-2 " style="display: table-cell; vertical-align: middle;height: 50px; ">
@Html.LabelFor(x => x.Label, Model.Label, new { @class = "control-label pull-right" })
</td>
<td class=" col-sm-2 " style="display: table-cell; vertical-align: middle;height: 50px; ">
@Html.DropDownListFor(m => m.Comparators, new SelectList(Model.Comparators, "Value", "Text"), new { @class = "form-control", style = "width:150px" })
</td>
<td class="col-sm-8 form-inline" style="display: table-cell; vertical-align: middle;height: 50px;">
@Html.TextBoxFor(x => x.Value, new { @class = "form-control" })
</td>
</tr>
</table>
我对索引的剃须刀视图如下
@model GenericSearch.Models.Search
<div class="row">
@using (@Html.BeginForm("Index", "Home", FormMethod.Post))
{
{
@Html.EditorFor(m => m.List)
<div class="form-group">
<div class="col-sm-12 text-center">
<button type="submit" class="btn btn-primary">Apply filters</button>
</div>
</div>
}
}
</div>
控制器上的两个Index动作如下
public ActionResult Index()
{
Search search = new Models.Search();
search.List= new List<BaseSearch>();
TextSearch text = new TextSearch();
text.Label = "Text1";
search.MyProperty.Add(text);
return View(search);
}
[HttpPost]
public ActionResult Index(Search search)
{
return View(search);
}
此设置呈现正常,但是当我填充渲染的文本框并通过提交按钮发布时,我希望收到填充了Search
的{{1}}类,该类填充了属性用户在HttpPost索引中输入的值。但是我得到一个错误,说明这是不可能的,因为List<TextSearch>
是抽象的,当我从BaseClass
删除抽象关键字时BaseClass
类被实例化但是{{1}而不是Search
它的属性也是空的,没有填入用户输入的数据。我在这里缺少什么?
修改
List<BaseSearch>