简体   繁体   中英

ASP.NET Razor Page Select List Loses Data after Server-Side Validation Failed

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();
    }

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();
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM