1
Vote

ModelState.IsGroupValid Throw exception: System.NotSupportedException: There are no model properties defined as part of validation group 'Users'

description

Assume we have a ViewModel as below:
public class UserListViewModel
{
    public List<UserInfo> Users { get; set; }
}
  • The UserInfo:
    public class UserInfo
    {
    public Guid Id { get; set; }
    
    [Required]
    [ValidationGroup("Users")]
    public string UserName { get; set; }
    }
  • The controller/action:
    [HttpPost]
    public ActionResult SaveUsers(HomeViewModel viewModel)
    {
        ModelState.Clear();
        if (ModelState.IsGroupValid(viewModel.UserListViewModel, "Users")) //Exception Here
        {
            //TODO: Business here!
            foreach (var userInfo in viewModel.UserListViewModel.Users)
            {
                userInfo.UserName = "Changed";
            }
        }
        return View("Index", viewModel);
    }
    When the user click Submit Button, the Validation method failed to traverse into sub-properties of "Users" and throw exception: System.NotSupportedException: There are no model properties defined as part of validation group.
    Is there anyway of how to fix it?
    Thanks & Regards

comments