Question

Trying to move some code from an MVC3 project to an MVC4 project, and running into this issue. There are some DisplayTemplates within an area, like so:

MVC Project
--Areas
----Identity
------Controllers
--------MyHomeController.cs
------Models
--------MyHomeViewModel.cs
--------MyEmailAddress.cs
------Views
--------Shared
----------DisplayTemplates
------------MyEmailAddress.cshtml
----------MyHome.cshtml
--------ViewStart.cshtml
--------Web.config
------IdentityAreaRegistration.cs

The MyHomeController has an action which returns the MyHome.cshtml view, strongly-typed to the MyHomeViewModel. MyHomeViewModel contains an IEnumerable<MyEmailAddress> property. Within the 'MyHome.cshtml' file, I have the following:

@foreach (var email in Model.Emails)
{
    @Html.DisplayFor(m => email)
}

However, this is not rendering the MyEmailAddress.cshtml Template. Same results with both of the following:

@foreach (var email in Model.Emails)
{
    @Html.DisplayFor(m => email, "MyEmailAddress")
}

@foreach (var email in Model.Emails)
{
    @Html.DisplayFor(m => email, "~/Areas/Identity/[rest of path]/MyEmailAddress.cshtml")
}

However, if I move the MyEmailAddress.cshtml file into the root web project's Views/Shared/DisplayTemplates folder, it does render using this template.

For some reason, the DisplayFor HtmlHelper is either not looking in the area-specific Views/Shared/DisplayTemplates folder, or it is looking for something else in that folder.

What am I doing wrong? This works in MVC3, but I can't seem to get it right in the MVC4 project.

Was it helpful?

Solution

Once again I immediately found my own answer right after giving up and posting this question.

The MyHomeController was using AttributeRouting. I discovered that if I went to the URL /Identity/MyHome/Action, the DisplayTemplate was being successfully chosen. However for my custom route, /my/home, it was not. Adding the following attribute to MyHomeController fixed this:

[RouteArea("Identity")]
public class MyHomeController : Controller
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top