ProfK
ProfK

Reputation: 51063

Why is my editor template simply not rendering?

This is my editor template for displaying a fixed list of provinces with a checkbox for each province:

@model Comair.RI.UI.Models.ApplicantRelocateProvinceList
<table>
    <tr>
        <th style="display: none;"></th>
        <th>
            @Html.DisplayNameFor(model => model.HeaderItem.Province)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.HeaderItem.WillRelocate)
        </th>
    </tr>
    @foreach (var item in Model.Items)
    {
        @Html.EditorFor(m => item)
    }
</table>

This is the editor template for the Model.Items type model:

@model Comair.RI.UI.Models.ApplicantRelocateProvinceItem
<tr>
    <td style="display: none;">
        @Html.HiddenFor(m => m.Id)
    </td>
    <td>
        @Html.DisplayFor(m => m.Province.Name)
    </td>
    <td>
        @Html.EditorFor(m => m.WillRelocate)
    </td>
</tr>

I have used this model successfully for other view models. Here is the ApplicantRelocateProvinceList model:

public class ApplicantRelocateProvinceList : ViewModel
{        
    public ApplicantRelocateProvinceItem HeaderItem { get; set; }
    public ApplicantRelocateProvinceList()
    {
        HeaderItem = new ApplicantRelocateProvinceItem();
    }
    public void MapFromEntityList(IEnumerable<ApplicantRelocateProvince> applicantProvinces)
    {
        var service = new ProvinceService(DbContext);
        var selectedIds = applicantProvinces.Select(ap => ap.ProvinceId);
        Items = service.ReadProvinces()
                       .Where(i => !i.IsDeleted)
                       .Select(p => new ApplicantRelocateProvinceItem {Id = p.Id, Province = p, WillRelocate = selectedIds.Contains(p.Id)});
    }
    public IEnumerable<ApplicantRelocateProvinceItem> Items { get; set; }
}

This code reads a fixed list of our nine provinces, and a variable list of provinces chosen by the applicant. Those of the fixed provinces whose IDs are in the applicant's provinces are marked with their WillRelocate flag as true, to show its checkbox as checked.

Upvotes: 1

Views: 4525

Answers (1)

webdeveloper
webdeveloper

Reputation: 17288

I think problem could be in this line:

@Html.EditorFor(m => item)

Try this:

@Html.EditorForModel(item)

Similar question: Why not use Html.EditorForModel()

Added:

Special folder must be named EditorTemplates, parent folder must be named as controller (for example Home) or Shared (for all controllers) and view name must be ApplicantRelocateProvinceItem.cshtml as class name.

Upvotes: 2

Related Questions