MVC3 Editor Template for List Not Populating
Problem: When I load the EditorTemplate for UserModel the UserRoles list does not generate a list of UserRole editors.
My models:
public class UserSearchModel
{
[ScaffoldColumn(false)]
public Guid UserId { get; set; }
[DisplayName("User Name")]
public string UserName { get; set; }
[DisplayName("User Roles")]
public UserRoles UserRoles { get; set; }
}
public partial class UserRoles : List<UserRole>
{
}
public class UserRole
{
public string RoleName { get; set; }
public bool IsChecked { get; set; }
public string Description { get; set; }
}
My editor Templates:
@model eGate.BackOffice.WebClient.Model.UserModel
@Html.LabelFor(m=>m.UserName)
@Html.EditorFor(m=>m.UserName)
<h1>roles</h1>
@Html.EditorFor(m=>m.UserRoles)
@model eGate.BackOffice.WebClient.Model.UserRole
@Html.LabelFor(m=>m.RoleName)
@Html.LabelFor(m=>m.IsChecked)
@Html.CheckBoxFor(m=>m.IsChecked)
@Html.LabelFor(m=>m.Description)
Actual Output Looks like:
User Name [ someuser ]
roles
Expected Output:
User Name开发者_运维问答 [ someuser ]
roles
role1 [x] ischecked role for 1
role2 [x] ischecked role for 2
role3 [x] ischecked role for 3
You should just make the type List rather than deriving your own type.
I expect that the problem is the definition of UserRoles
as a separate class. Try changing the property UserRoles
in the UserSearchModel
class to:
public List<UserRole> UserRoles { get; set; }
MVC is searching for a template for the UserRoles
class. Since the template is for the UserRole
class, MVC fails to find any template.
UPDATE
Also, I overlooked that you are using EditorFor
for the UserRoles model. You should use EditorForModel
like so:
@Html.EditorForModel(m => m.UserRoles)
This issue looks to be more likely caused by the fact the User EditorTemplate is being served up by the popUp window of a telerik mvc grid.
I have tested outside the grid popup to find that the templates work as expected. I will close this question and re-ask the correct question in another entry.
精彩评论