MVC EditorTemplate列表不属于模型

时间:2012-03-07 08:16:42

标签: model-view-controller mvc-editor-templates editortemplates

在我的视图中,我调用了List的partialview。在该局部视图中,我将该列表分成两个IEnumerables,对于每个列表,我想为ModelType调用EditorTemplate:

我的偏见:

@model List<ModelType>

@using System.Collections;         

@{
    int countModelTypeLeft = (Model.Count % 2 != 0) ? Model.Count + 1 : Model.Count ;
    int countModelTypeRight = Model.Count;

    IEnumerable<ModelType> modelTypeListLeft = Model.Take(countModelTypeLeft);
    IEnumerable<ModelType> modelTypeListRight = Model.Range(countModelTypeLeft , countModelTypeRight );
}
    <div class="modeltype-left" style="float: left; width: 50%;">
        // How can I call EditorFor for modelTypeListLeft  now?
    </div>

    <div class="modeltype-right" style="float: right; width: 50%;">
        // How can I call EditorFor for modelTypeListRight  now?
    </div>

正如您所看到的,我被卡住了因为我无法调用EditorFor,因为两个列表中的modelTypeListLeft和countModelTypeRight不是局部视图中给定模型的一部分。如何解决这个问题?

1 个答案:

答案 0 :(得分:2)

如果您有ModelType的编辑器模板,那么这仍然可以使用正确的编辑器模板

<div class="modeltype-left" style="float: left; width: 50%;">        
    @foreach(var leftItem in modelTypeListLeft )
    {
        Html.EditorFor(m=>leftItem)
    }
</div>

<div class="modeltype-right" style="float: right; width: 50%;">        
    @foreach(var rightItem in modelTypeListRight)
    {
        Html.EditorFor(m=>rightItem)
    }
</div>