@for (var i = 0; i < Model.Roles.Count; i++)
{
<div class="row" style="margin-bottom: .5rem;">
<div class="col-lg-3"></div>
<div class="col-lg-3">
<li class="list-group-item" style="background: #f5f6f7; border: none;">
@Html.Hidden("Roles[" + i + "].id", Model.Roles[i].id)
<label for="Roles_@(i)_ischecked" class="label">
@Html.CheckBox("Roles[" + i + "].ischecked", Model.Roles[i].ischecked)
@Model.Roles[i].name
</label>
</li>
</div>
</div>
}
异常详细信息:System.NullReferenceException:未将对象引用设置为对象的实例
但是通过这些代码引用了真实对象
return View(new UsersNew
{
Roles = Database.Session.Query<Role>().Select(role => new RoleCheckbox
{
id = role.id,
ischecked = false,
name = role.name
}).ToList()
})
我甚至引用了我的视图,如下所示
@model EtlGui.ViewModels.UsersNew
我的ViewModel看起来像这样
public class UsersNew
{
public IList<RoleCheckbox> Roles { get; set; }
[Required,MaxLength(128)]
public string username { get; set; }
[Required,DataType(DataType.Password)]
public string password { get; set; }
[Required,MaxLength(256),DataType(DataType.EmailAddress)]
public string email { get; set; }
}
我在这里做错了什么?