EditorFor的MVC模型绑定

时间:2017-10-20 10:36:03

标签: asp.net-mvc model-binding editorfor

我有以下关系的三种类型SearchBaseSearchTextSearchSearchList<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>

0 个答案:

没有答案