2011-04-08 59 views
0

我下面这个答案here,使用此代码:多个复选框3剃刀

SomeControl.cshtml

@model Econo.WebUI.ViewModel.UserRoleViewModel 

@Html.HiddenFor(x => Model.UserId) 

@Html.EditorFor(x => Model.Roles) 

/Shared/EditorTemplates/Roles.cshtml

@model Econo.WebUI.ViewModel.RoleViewModel 

@Html.EditorFor(m => m.IsInRole) 
@Html.EditorFor(m => m.RoleId) 
@Html.EditorFor(m => m.RoleName) 

由于某种原因,此输出仅为:

<input id="UserId" type="hidden" value="1LS82" name="UserId"> 
FalseFalse 

FalseFalse显示数据库中有2个角色,用户不在其中,这是正确的。但我需要一个带有ID和名称的复选框。还能够将其提交回服务器以向用户添加角色。

回答

1

您的部分错误地命名。它应该是~/Shared/EditorTemplates/RoleViewModel.cshtml而不是~/Shared/EditorTemplates/Roles.cshtml。然后:

@model Econo.WebUI.ViewModel.RoleViewModel 

@Html.CheckBoxFor(m => m.IsInRole) 
@Html.LabelFor(m => m.RoleName) 
@Html.HiddenFor(m => m.RoleId)