I read that EditorTemplates are loaded automatically, but from asp.net mvc 2 and now 3 with razor, I still cant get this to work.
My model looks like this:
public class RoleViewModel
{
public int RoleId { get; set; }
public bool InRole { get; set; }
public string RoleName { get; set; }
}
public class UserViewModel
{
public User User { get; set; }
public IEnumerable<RoleViewModel> Roles { get; set; }
}
My view looks like this:
~/Views/Roles/Edit.cshtml
@model Project.Web.ViewModel.UserViewModel
@using (Html.BeginForm()) {
@Html.EditorFor(model => model.Roles)
<!-- Other stuff here -->
}
~/Views/Roles/EditorTemplates/RoleViewModel.cshtml
@model Project.Web.ViewModel.RoleViewModel
@foreach (var i in Model)
{
<div>
@i.RoleName
@Html.HiddenFor(model => i.RoleId)
@Html.CheckBoxFor(model => i.InRole)
</div>
}
If i move the content from the EditorTemplate
to the actual page, then it works, it shows the checkbox, etc. But with this current setup, all that shows up is the count of the number of roles.
What am I doing wrong?