此处的问题是,在提交表单时,UserCustomerClaims
对象中的ApplicationUserEditViewModel
属性列表为null
; for循环中的复选框的响应没有或正在进入控制器。
查看
@model WaterManagementSystem.Features.Admin.ApplicationUserEditViewModel
@{
ViewBag.Title = "Edit User";
Layout = "_Layout";
}
<form asp-action="UpdateUser" asp-controller="Admin" method="post">
...
<div style="color: White;">
@foreach (var userClaim in Model.UserCustomerClaims)
{
<div>
<input type="checkbox" asp-for="@userClaim.HasClaim" id="@userClaim.ID" name="@userClaim.ID" checked="@(userClaim.HasClaim)" />
<input type="hidden" asp-for="@userClaim.ID" /> @userClaim.UserClaim.Type
</div>
}
</div>
<div>
<div>
<button type="submit" class="btn btn-default">Save</button>
</div>
</div>
</form>
查看模型
public class ApplicationUserEditViewModel : ApplicationUserViewModel
{
...
private List<UserCustomerClaim> userCustomerClaims_ = null;
public ApplicationUserEditViewModel() : this(new ApplicationUser())
{
userCustomerClaims_ = new List<UserCustomerClaim>();
}
public List<UserCustomerClaim> UserCustomerClaims
{
get
{
return userCustomerClaims_;
}
}
public class UserCustomerClaim
{
public UserCustomerClaim(Claim userClaim, bool hasClaim)
{
UserClaim = userClaim;
HasClaim = hasClaim;
ID = Guid.NewGuid();
}
public Guid ID { get; set; }
public Claim UserClaim { get; set; }
public bool HasClaim { get; set; }
}
}
控制器
[HttpPost]
public async Task<ActionResult> UpdateUser(ApplicationUserEditViewModel vm)
{
...
}
建议的副本不使用asp-for
标记助手。