MattNorman
Well-known member
- Joined
- May 22, 2021
- Messages
- 98
- Programming Experience
- 1-3
I have a user model as follows:
This model contains a list of 'DTOUserWalletPermission' which is defined as follows:
I have a table in my view that displays the contents of the 'WalletPermissions' property.
When posting the data back the model state shows as invalid specifically for the 'WalletName' property within the user model. I don't understand this as that property does not have the [Required] attribute. Even if I assign that property a value before checking the model state, it still returns as not valid.
What I am essentially trying to achieve is to have the wallet permission data displayed in a table and allow the three bool values to be changed using checkboxes without the other properties being editable. I can match the missing data back up in the controller but just need the model validation to work correctly.
Can anyone help?
C#:
public sealed class DTOUser
{
public int UserId { get; set; }
[Required]
[Display(Name = "Username (*)")]
public string Username { get; set; } = null!;
[Required]
[JsonIgnore]
[Display(Name = "Password (*)")]
public string Password { get; set; } = null!;
[Required]
[Display(Name = "First Name (*)")]
public string FirstName { get; set; } = null!;
[Required]
[Display(Name = "Last Name (*)")]
public string LastName { get; set; } = null!;
[Required]
[EmailAddress]
[Display(Name = "Email (*)")]
public string Email { get; set; } = null!;
[Required]
public bool Locked { get; set; }
[Required]
public bool IsTempPassword { get; set; }
public bool ChangePassword { get; set; }
public bool IsEdit { get; set; }
public bool EmailPassword { get; set; }
[Required]
[Display(Name = "Role (*)")]
public int UserRoleId { get; set; }
// Used to hold select list values for user role.
public IEnumerable<SelectListItem>? UserRoleSelectList { get; set; }
// Used to hold users wallet permissions.
public List<DTOUserWalletPermission> WalletPermissions { get; set; } = new();
public override string ToString()
{
return JsonConvert.SerializeObject(this, Formatting.Indented);
}
}
This model contains a list of 'DTOUserWalletPermission' which is defined as follows:
C#:
public sealed class DTOUserWalletPermission
{
public int UserWalletPermissionsId { get; set; }
public int UserId { get; set; }
public int WalletId { get; set; }
public bool AddAccounts { get; set; }
public bool EditAccounts { get; set; }
public bool DeleteAccounts { get; set; }
public string WalletName { get; set; } = null!;
}
I have a table in my view that displays the contents of the 'WalletPermissions' property.
When posting the data back the model state shows as invalid specifically for the 'WalletName' property within the user model. I don't understand this as that property does not have the [Required] attribute. Even if I assign that property a value before checking the model state, it still returns as not valid.
What I am essentially trying to achieve is to have the wallet permission data displayed in a table and allow the three bool values to be changed using checkboxes without the other properties being editable. I can match the missing data back up in the controller but just need the model validation to work correctly.
Can anyone help?