'"mes":"Value cannot be null. (Parameter 'source')" when want to show value which is true in view with checkbox
Hi I have 3 tables: Role , Permission , RolePermission In my Razor page view I want to show each permission that is true for a special role with marking the check box . In every role I show all permission in a tree model (my permission is a parent child table) So for this reason I make a model
[Table("tblPermission", Schema = "cmn")]
public class Permission
{
[Key]
public int PermissionId { get; set; }
public string PermissionTitle { get; set; }
public int? ParentId { get; set; }
[ForeignKey("ParentId")]
public List<Permission> Permissions { get; set; }
public List<RolePermission> RolePermissions { get; set; }
}
And two action
[BindProperty]
public Role Role { get; set; }
public void OnGet(int id)
{
Role = _permissionService.GetRoleById(id);
var b= _permissionService.GetAllPermission();
ViewData["Permissions"] = b;
var a= _permissionService.permissionsRole(id);
ViewData["SelectedPermission"] = a;
}
public IActionResult OnPost()
{
if (!ModelState.IsValid)
{
return Page();
}
_permissionService.UpdateRole(Role);
return RedirectToPage("Index");
}
And my view is like bellow :
@page "{id}"
@using Entities.Permission
@model Pages.Admin.Roles.EditRoleModel
@{
ViewData["Title"] = "Edit role";
List<Permission> permissions = ViewData["Permissions"] as List<Permission>;
List<int> SelectedPermissions = ViewData["SelectedPermissions"] as List<int>;
}
<div class="panel-body">
<ul>
@foreach (var permission in permissions.Where(p => p.ParentId == null))
{
<li>
<input type="checkbox" name="SelectedPermission"
@((SelectedPermissions.Any(p=>p==permission.PermissionId)?"checked":""))
value="@permission.PermissionId" /> @permission.PermissionTitle
@if (permissions.Any(p => p.ParentId == permission.ParentId))
{
<ul>
@foreach (var sub in permissions.Where(p => p.ParentId == permission.PermissionId))
{
<li>
<input type="checkbox" name="SelectedPermission"
@((SelectedPermissions.Any(p=>p==sub.PermissionId)?"checked":"")) value="@sub.PermissionId"
/> @sub.PermissionTitle
</li>
}
</ul>
}
</li>
}
</ul>
</div>
At this line “@((SelectedPermissions.Any(p=>p==sub.PermissionId)?"checked":""))” I get the error : "mes":"Value cannot be null. (Parameter 'source')" .And when I Check out I found that none of my viewData is not null.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|