skip to Main Content

I am using .NET Identity for authentication and authorization.
For my registration page, I added two selectListItem properties in the InputModel class for dropdown lists.

The problem is, when the server-side validation failed, the dropdown lists lost their data as the page reloaded. Other basic data are saved.

I consulted several old posts on how to repopulate the dropdown list but still can’t solve the problem. I don’t know what exactly is being executed after the return Page() is called.

Thanks in advance.

Here’s page model and methods:

public class InputModel
    {
        ......
        [Required]
        public string Name { get; set; }
        ......
        [ValidateNever]
        public IEnumerable<SelectListItem> RoleList { get; set; }
        [ValidateNever]
        public IEnumerable<SelectListItem> CompanyList { get; set; }
    }




public async Task OnGetAsync(string returnUrl = null)
    {
        ......
        ......
        Input = new InputModel()
        {
            RoleList = _roleManager.Roles.Select(x => x.Name).Select(i => new SelectListItem
            {
                Text = i,
                Value = i
            }),
            CompanyList = _unitOfWork.Company.GetAll().Select(i => new SelectListItem
            {
                Text = i.Name,
                Value = i.Id.ToString()
            })
        };
    }




public async Task<IActionResult> OnPostAsync(string returnUrl = null)
    {
        ......
        
        if (ModelState.IsValid)
        {
            var user = CreateUser();

            await _userStore.SetUserNameAsync(user, Input.Email, CancellationToken.None);
            await _emailStore.SetEmailAsync(user, Input.Email, CancellationToken.None);
            user.StreetAddress = Input.StreetAddress;
            user.City = Input.City;
            user.State = Input.State;
            user.PostalCode = Input.PostalCode;
            user.Name = Input.Name;
            user.PhoneNumber = Input.PhoneNumber;
            
            if(Input.Role == SD.Role_User_Comp)
            {
                user.CompanyId = Input.CompanyId;
            }
            var result = await _userManager.CreateAsync(user, Input.Password);

            if (result.Succeeded)
            {
                ......
                ......
            }
            foreach (var error in result.Errors)
            {
                ModelState.AddModelError(string.Empty, error.Description);
            }
            
                
        }

      
        // If we got this far, something failed, redisplay form
        return Page();
    }

2

Answers


  1. You can try to set RoleList and CompanyList into OnPostAsync:

    public async Task<IActionResult> OnPostAsync(string returnUrl = null)
        {
            ......
            
            if (ModelState.IsValid)
            {
                var user = CreateUser();
    
                await _userStore.SetUserNameAsync(user, Input.Email, CancellationToken.None);
                await _emailStore.SetEmailAsync(user, Input.Email, CancellationToken.None);
                user.StreetAddress = Input.StreetAddress;
                user.City = Input.City;
                user.State = Input.State;
                user.PostalCode = Input.PostalCode;
                user.Name = Input.Name;
                user.PhoneNumber = Input.PhoneNumber;
                
                if(Input.Role == SD.Role_User_Comp)
                {
                    user.CompanyId = Input.CompanyId;
                }
                var result = await _userManager.CreateAsync(user, Input.Password);
    
                if (result.Succeeded)
                {
                    ......
                    ......
                }
                foreach (var error in result.Errors)
                {
                    ModelState.AddModelError(string.Empty, error.Description);
                }
                
                    
            }
             RoleList = _roleManager.Roles.Select(x => x.Name).Select(i => new SelectListItem
                {
                    Text = i,
                    Value = i
                });
                CompanyList = _unitOfWork.Company.GetAll().Select(i => new SelectListItem
                {
                    Text = i.Name,
                    Value = i.Id.ToString()
                });
          
            // If we got this far, something failed, redisplay form
            return Page();
        }
    
    Login or Signup to reply.
  2. Also suffering from the same problem for the last few days. I solved it by using Yiyi You’s method. Though I made a different function for assigning values to the Properties (in your case RoleList and CompanyList) then call it in both OnPostAsync and OnGetAsync

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search