ASP.NET Razor页选择列表在服务器端验证失败后丢失数据

7dl7o3gd  于 2022-12-05  发布在  .NET
关注(0)|答案(2)|浏览(135)

我使用.NET Identity进行身份验证和授权。对于我的注册页面,我在InputModel类中为下拉列表添加了两个selectListItem属性。
问题是,当服务器端验证失败时,下拉列表会在页面重新加载时丢失数据,而其他基本数据会被保存。
我查阅了几篇关于如何重新填充下拉列表的旧帖子,但仍然不能解决这个问题。我不知道在调用return Page()后究竟执行了什么。
先谢谢你。
下面是页面模型和方法:

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

vlju58qv1#

您可以尝试将RoleList和CompanyList设置为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();
    }
vsdwdz23

vsdwdz232#

前几天也遇到了同样的问题。我用YiyiYou的方法解决了这个问题。虽然我做了一个不同的函数来给属性赋值(在你的例子中是RoleList和CompanyList),然后在OnPostAsyncOnGetAsync中调用它

相关问题