ASP.NET MVC 5.1 EditorFor and DisplayFor not using custom templates

all of a sudden my MVC application stopped using custom templates EditorForor DisplayForthat I have. I'm not sure exactly when this happened, since I changed the user interface. I have templates sitting in DisplayTemplatesand EditorTemplatesin a folder Shared. I override ViewEnginesCollectionin the Global.asaxfollowing way:

ViewEngines.Engines.Clear();
ViewEngines.Engines.Add(new CSHtmlRazorViewEngine {
    PartialViewLocationFormats = new string[] { 
        "~/Views/Shared/EditorTemplates/{0}.cshtml",
        "~/Views/Shared/Partials/{0}.cshtml"
    }
});

Where CSHtmlRazorViewEngine:

public sealed class CSHtmlRazorViewEngine : RazorViewEngine {
    public CSHtmlRazorViewEngine()
        : base() {
        this.AreaViewLocationFormats = new string[2] {
            "~/Areas/{2}/Views/{1}/{0}.cshtml",
            "~/Areas/{2}/Views/Shared/{0}.cshtml"
        };
        this.AreaMasterLocationFormats = new string[2] {
            "~/Areas/{2}/Views/{1}/{0}.cshtml",
            "~/Areas/{2}/Views/Shared/{0}.cshtml"
        };
        this.AreaPartialViewLocationFormats = new string[2] {
            "~/Areas/{2}/Views/{1}/{0}.cshtml",
            "~/Areas/{2}/Views/Shared/{0}.cshtml"
        };
        this.ViewLocationFormats = new string[3] {
            "~/Views/{0}.cshtml",
            "~/Views/{1}/{0}.cshtml",
            "~/Views/Shared/{0}.cshtml"
        };
        this.MasterLocationFormats = new string[2] {
            "~/Views/{1}/{0}.cshtml",
            "~/Views/Shared/{0}.cshtml"
        };
        this.PartialViewLocationFormats = new string[2] {
            "~/Views/{1}/{0}.cshtml",
            "~/Views/Shared/{0}.cshtml"
        };
        this.FileExtensions = new string[1] {
            "cshtml"
        };
    }
}

It’s a little difficult for me to understand where I made a mistake suddenly. Any recommendations on where to check that?

UPDATE - code examples

Here Edit.cshtmlfor the object Office:

<div class="Section">
    @using (Html.BeginForm("Edit", "Offices", new {
        id = Model.Office.Id
    }, FormMethod.Post)) {
        <div>
            <input type="submit" value="Save" />
        </div>
        @Html.EditorFor(m => m.Office, new {
            Regions = Model.Regions,
            States = Model.States
        })
    }
    @Html.Partial("Equipments", Model.Equipments)
</div>

And here is the template EditorForfor Officewhich is requested:

@model Office
<p>
    @Html.Label("Name", "Name:")
    @Html.TextBox("Name", Model.Name, new {
        required = string.Empty
    })
</p>
<p>
    @Html.Label("RegionId", "Region:")
    @Html.DropDownList("RegionId", new SelectList((IEnumerable<Region>)ViewData["Regions"], "Id", "Name", Model.RegionId), string.Empty, new {
        required = string.Empty
    })
</p>
@Html.EditorFor(m => m.Address)

And here OfficesController.Edit() ActionResult:

[HttpGet]
public async Task<ActionResult> Edit(
    short id) {
    if (id > 0) {
        Office office = await base.Repository.FindSingleOrDefaultAsync<Office, short>(id);

        if (office != null) {
            Task<IQueryable<Equipment>> equipments = Task.Run(() => base.Repository.FindEquipment<Office>(id));
            Task<IQueryable<Region>> regions = Task.Run(() => base.Repository.Find<Region>());
            Task<IQueryable<State>> states = Task.Run(() => base.Repository.Find<State>());

            await Task.WhenAll(equipments, regions, states);

            return base.View(new OfficesView {
                Equipments = equipments.Result.ToList(),
                Office = office,
                Regions = regions.Result,
                States = states.Result,
                ViewData = new OfficeViewData {
                    Map = new Map {
                        Center = office.Address.Position.ToPoint(),
                        Polygons = (office.Boundaries != null) ? new Polygon[] {
                            office.Boundaries.ToPolygon()
                        } : null
                    }
                }
            });
        }
    }

    return base.RedirectToAction("List");
}

. EditorFor . .

+2
1

MVC EditorTemplates/ . , ExecuteTemplate.

:

"~/Views/Shared/EditorTemplates/{0}.cshtml"
"~/Views/Shared/Partials/{0}.cshtml"

Address MVC EditorTemplates/Address . , 2 :

~/Views/Shared/EditorTemplates/EditorTemplates/Address.cshtml
~/Views/Shared/Partials/EditorTemplates/Address.cshtml

, .

, EditorTemplates?

+4

All Articles