Search code examples
model-view-controllermvc-editor-templateseditortemplates

MVC EditorTemplate for list that is not part of the model


Within my view I call a partialview for a List. In that partial view I seperate that list into two IEnumerables and for each list I want to call the EditorTemplate for ModelType:

My PartialView:

@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>

As you can see, I am stuck because I can't call EditorFor because the two Lists modelTypeListLeft and countModelTypeRight are not part of the given Model in the partial view. How to solve this problem?


Solution

  • if you have a editor template for ModelType, then this will still work and use the correct editor template

    <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>