Dear Sir.
I have ViewModel
public class UserRole
{
public String RoleName;
public bool IsInRole;
}
public class UserEditViewModel
{
[Required, MaxLength(256)]
public String UserName { get; set; }
...
public UserRole[] Roles { get; set; }
}
RazorView
@for(int i=0; i< @Model.Roles.Length; i++)
{
<label class="checkbox"><input asp-for="@Model.Roles[i].IsInRole"
type="checkbox"
value="@Model.Roles[i].IsInRole"
>@Model.Roles[i].RoleName</label>
<input type="hidden" asp-for="@Model.Roles[i].RoleName" />
}
Controller
public async Task<IActionResult> Edit(String id, UserEditViewModel model)
{
...
if (model != null && ModelState.IsValid)
{
throw new Exception("ZERO: ["+ (model.Roles == null).ToString() + "] " + model.Roles.Length.ToString() + model.Roles.Count().ToString());
}
...
}
But My Array has 0 Elements
although
ViewModel:
public class EditUserRole
{
public String RoleName;
public bool IsInRole;
}
public class UserEditViewModel
{
[Required, MaxLength(256)]
public String UserName { get; set; }
...
public EditUserRole[] Roles { get; set; }
}
View:
<div style="padding-left:30px" class="panel-body">
@for(int i=0; i< @Model.Roles.Length; i++)
{
<label class="checkbox"><input asp-for="@Model.Roles[i].IsInRole"
type="checkbox"
value="@Model.Roles[i].IsInRole"
>@Model.Roles[i].RoleName</label>
<input type="hidden" asp-for="@Model.Roles[i].RoleName" />
}
</div>
Controler:
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Edit(String id, UserEditViewModel model)
{
...
if (model != null && ModelState.IsValid)
{
throw new Exception("ZERO: ["+ (model.Roles == null).ToString() + "] " + model.Roles.Length.ToString() + model.Roles.Count().ToString());
}
...
}
But I Still have model.Roles Array with size 0 Although My rendered documents
has 4 element before sumbit
<div style="padding-left:30px" class="panel-body">
<label class="checkbox"><input data-val="true" data-val-required="The Boolean field is required." id="Roles_0__IsInRole" name="Roles[0].IsInRole" type="checkbox" value="False">update administrator</label>
<input type="hidden" id="Roles_0__RoleName" name="Roles[0].RoleName" value="update administrator" />
<label class="checkbox"><input data-val="true" data-val-required="The Boolean field is required." id="Roles_1__IsInRole" name="Roles[1].IsInRole" type="checkbox" value="False">super administrator</label>
<input type="hidden" id="Roles_1__RoleName" name="Roles[1].RoleName" value="super administrator" />
<label class="checkbox"><input data-val="true" data-val-required="The Boolean field is required." id="Roles_2__IsInRole" name="Roles[2].IsInRole" type="checkbox" value="False">update power user</label>
<input type="hidden" id="Roles_2__RoleName" name="Roles[2].RoleName" value="update power user" />
<label class="checkbox"><input data-val="true" data-val-required="The Boolean field is required." id="Roles_3__IsInRole" name="Roles[3].IsInRole" type="checkbox" value="False">super power user</label>
<input type="hidden" id="Roles_3__RoleName" name="Roles[3].RoleName" value="super power user" />
</div>
Please Help me I'm doing wrong ?