Ends in:
0 DAY
19 HRS
25 MIN
30 SEC
Ends in:
0 D
19 H
25 M
30 S
Open Closed

Variables Not Holding Values fetched from Database #6368


User avatar
0
pvala created
  • ABP Framework version: v7.3.2
  • UI Type: Angular / MVC
  • Database System: EF Core (MySQL)
  • Tiered (for MVC) or Auth Server Separated (for Angular): yes/no
  • Exception message and full stack trace:
  • Steps to reproduce the issue:

I am working on the Auth Server project (independent). I have a requirement where I want to put some extra input fields on the Registration page of auth server. I'd collect the values of those extra input fields and will store those values in my other database table (separate from the ones provided by ABP). I am even able to carry out the whole process and am successfully able to store the data in the database without altering the existing configuration. I am fetching the details of those input fields from my database and storing them in a variable in the Register.cshtml.cs file. And I am using that variable to display those input fields on UI.

Now, here the problem is, that if I have any kind of a validation error (username already taken for example), and if I try to submit at this point, the application will pop out the validation error, saying this username is already taken, which is fine. But all my input fields get disappeared as soon as there is some validation error. I think this is happening because the class is losing all of it's values as soon as there is any validation error and hence it resets all the variables of the class and hence my variable (which stores the input field details), also gets reset and therefore loses all the details and it doesn't appear on the UI.

Is there a way to keep the value of that variable held during the entire time? Any kind of annotation or something? Please suggest.


8 Answer(s)
  • User Avatar
    0
    liangshiwei created
    Support Team Fullstack Developer

    I could not reproduce the problem.

    Can you share how you did it and some screenshots?

  • User Avatar
    0
    pvala created

    Sure, here it is.

    If you see here in the following form, you'd see 2 input fields i.e., Name and Gender, the details of which are being fetched from the database from an API endpoint. And those fields will be generated dynamically on run time.

    Now, once I put all the details, and click on Register, those 2 fields will be disappeared.

    Here is my code as well :

    Register.cshtml.cs file :

    using System;
    using System.Collections.Generic;
    using System.ComponentModel.DataAnnotations;
    using System.Linq;
    using System.Security.Claims;
    using System.Threading.Tasks;
    using G1.health.ClinicService.ClinicSetup;
    using Microsoft.AspNetCore.Authentication;
    using Microsoft.AspNetCore.Http;
    using Microsoft.AspNetCore.Identity;
    using Microsoft.AspNetCore.Mvc;
    using Microsoft.AspNetCore.Mvc.Rendering;
    using Microsoft.Extensions.Logging;
    using Microsoft.Extensions.Options;
    using Newtonsoft.Json;
    using Owl.reCAPTCHA;
    using survey.FormsService.Forms;
    using Volo.Abp;
    using Volo.Abp.Account;
    using Volo.Abp.Account.Public.Web.Pages.Account;
    using Volo.Abp.Account.Public.Web.Security.Recaptcha;
    using Volo.Abp.Account.Settings;
    using Volo.Abp.AspNetCore.Mvc.MultiTenancy;
    using Volo.Abp.Auditing;
    using Volo.Abp.Data;
    using Volo.Abp.EntityFrameworkCore;
    using Volo.Abp.Identity;
    using Volo.Abp.Identity.EntityFrameworkCore;
    using Volo.Abp.Identity.Settings;
    using Volo.Abp.Security.Claims;
    using Volo.Abp.Settings;
    using Volo.Abp.Uow;
    using Volo.Abp.Validation;
    using Volo.Forms;
    using Volo.Forms.Questions;
    using IdentityUser = Volo.Abp.Identity.IdentityUser;
    
    namespace G1.health.AuthServer.Pages.Account;
    
    public class RegisterModel : Web.Pages.Account.AccountPageModel
    {
        public IAbpTenantAppService AbpTenantAppService { get; set; }
    
        public IFormsAppService FormsAppService { get; set; }
    
        public IUsersAppService UsersAppService { get; set; }
    
        private readonly IUnitOfWorkManager UnitOfWorkManager;
    
        public RegisterModel(IAbpTenantAppService abpTenantAppService, IFormsAppService formsAppService, IUsersAppService usersAppService, IUnitOfWorkManager unitOfWorkManager)
        {
            AbpTenantAppService = abpTenantAppService;
            FormsAppService = formsAppService;
            UsersAppService = usersAppService;
            UnitOfWorkManager = unitOfWorkManager;
        }
    
        [BindProperty(SupportsGet = true)]
        public string? ReturnUrl { get; set; }
    
        [BindProperty(SupportsGet = true)]
        public string? ReturnUrlHash { get; set; }
    
        [BindProperty(SupportsGet = true)]
        public Guid TenantId { get; set; }
    
        [BindProperty]
        public PostInput Input { get; set; }
    
        [BindProperty]
        public List<Field> Fields { get; set; }
    
        [BindProperty]
        public List<QuestionDto> QuestionDtos { get; set; }
    
        private async Task CreateExtraFields()
        {
            var tenant = await AbpTenantAppService.FindTenantByIdAsync(TenantId);
    
            if (tenant.TenantId != null)
            {
                var form = await FormsAppService.GetRegistrationFormAsync(TenantId);
                QuestionDtos = form.Questions;
            }
    
            Fields = new List<Field>();
    
            foreach (var item in QuestionDtos)
            {
                var choices = new List<SelectListItem>();
    
                foreach (var choice in item.Choices)
                {
                    choices.Add(new SelectListItem()
                    {
                        Text = choice.Value,
                        Value = choice.Value
                    });
                }
    
                Field field = new Field
                {
                    Index = item.Index,
                    Title = item.Title,
                    IsRequired = item.IsRequired,
                    HasOtherOption = item.HasOtherOption,
                    QuestionType = item.QuestionType,
                    Answer = null,
                    Choices = choices,
                    Answers = null
                };
                Fields.Add(field);
            }
        }
    
        [BindProperty(SupportsGet = true)]
        public bool IsExternalLogin { get; set; }
    
        public bool LocalLoginDisabled { get; set; }
    
        public bool UseCaptcha { get; set; }
    
        public virtual async Task<IActionResult> OnGetAsync()
        {
    
            //await ValidateForm();
    
            var localLoginResult = await CheckLocalLoginAsync();
            if (localLoginResult != null)
            {
                LocalLoginDisabled = true;
                return localLoginResult;
            }
    
            await CheckSelfRegistrationAsync();
            await TrySetEmailAsync();
            await SetUseCaptchaAsync();
    
            await CreateExtraFields();
    
            return Page();
        }
    
        [UnitOfWork] //TODO: Will be removed when we implement action filter
        public virtual async Task<IActionResult> OnPostAsync()
        {
            try
            {
                await CheckSelfRegistrationAsync();
                await SetUseCaptchaAsync();
    
                IdentityUser user;
                if (IsExternalLogin)
                {
                    var externalLoginInfo = await SignInManager.GetExternalLoginInfoAsync();
                    if (externalLoginInfo == null)
                    {
                        Logger.LogWarning("External login info is not available");
                        return RedirectToPage("./Login");
                    }
    
                    user = await RegisterExternalUserAsync(externalLoginInfo, Input.EmailAddress);
                }
                else
                {
                    var localLoginResult = await CheckLocalLoginAsync();
                    if (localLoginResult != null)
                    {
                        LocalLoginDisabled = true;
                        return localLoginResult;
                    }
    
                    user = await RegisterLocalUserAsync();
                }
    
                if (await SettingProvider.IsTrueAsync(IdentitySettingNames.SignIn.RequireConfirmedEmail) && !user.EmailConfirmed ||
                    await SettingProvider.IsTrueAsync(IdentitySettingNames.SignIn.RequireConfirmedPhoneNumber) && !user.PhoneNumberConfirmed)
                {
                    await StoreConfirmUser(user);
    
                    return RedirectToPage("./ConfirmUser", new
                    {
                        returnUrl = ReturnUrl,
                        returnUrlHash = ReturnUrlHash
                    });
                }
    
                await SignInManager.SignInAsync(user, isPersistent: true);
    
                return Redirect(ReturnUrl ?? "/"); //TODO: How to ensure safety? IdentityServer requires it however it should be checked somehow!
            }
            catch (BusinessException e)
            {
                Alerts.Danger(GetLocalizeExceptionMessage(e));
                return Page();
            }
        }
    
        [UnitOfWork]
        protected virtual async Task<IdentityUser> RegisterLocalUserAsync()
        {
            using (var unitOfWork = UnitOfWorkManager.Begin())
            {
                ValidateModel();
    
                var captchaResponse = string.Empty;
                if (UseCaptcha)
                {
                    captchaResponse = HttpContext.Request.Form[RecaptchaValidatorBase.RecaptchaResponseKey];
                }
    
                RegisterDto registerDto = new RegisterDto()
                {
                    AppName = "MVC",
                    EmailAddress = Input.EmailAddress,
                    Password = Input.Password,
                    UserName = Input.UserName,
                    ReturnUrl = ReturnUrl,
                    ReturnUrlHash = ReturnUrlHash,
                    CaptchaResponse = captchaResponse
                };
    
                foreach (var item in Fields)
                {
                    if (item.Answers != null)
                    {
                        string result = "{";
                        foreach (var answer in item.Answers)
                        {
                            result += '"' + answer + '"';
                            if (!(answer == item.Answers.LastOrDefault()))
                            {
                                result += ",";
                            }
                        }
                        result += "}";
                        item.Answer = result;
                    }
                    registerDto.SetProperty(item.Title, item.Answer);
                }
    
                var userDto = await AccountAppService.RegisterAsync(registerDto);
    
                RegisterUser registerUser = new RegisterUser(userDto.Id, Input.UserName, Input.EmailAddress, TenantId, userDto.CreationTime, "Patient", Input.Password);
    
                foreach (var key in Fields)
                {
                    switch (key.Title)
                    {
                        case "Name":
                            registerUser.first_name = key.Answer;
                            break;
                        case "Surname":
                            registerUser.last_name = key.Answer;
                            break;
                        case "DOB":
                            registerUser.dob = DateTime.Parse(key.Answer);
                            break;
                        case "Gender":
                            registerUser.gender = key.Answer;
                            break;
                        default:
                            break;
                    }
                }
    
                await UsersAppService.RegisterUser(registerUser);
    
                return await UserManager.GetByIdAsync(userDto.Id);
    
            }
        }
    
        private async Task<bool> ValidateForm()
        {
            return true;
        }
    
        protected virtual async Task<IdentityUser> RegisterExternalUserAsync(ExternalLoginInfo externalLoginInfo, string emailAddress)
        {
            await IdentityOptions.SetAsync();
    
            var user = new IdentityUser(GuidGenerator.Create(), emailAddress, emailAddress, CurrentTenant.Id);
    
            (await UserManager.CreateAsync(user)).CheckErrors();
            (await UserManager.AddDefaultRolesAsync(user)).CheckErrors();
    
            if (!user.EmailConfirmed)
            {
                await AccountAppService.SendEmailConfirmationTokenAsync(
                    new SendEmailConfirmationTokenDto
                    {
                        AppName = "MVC",
                        UserId = user.Id,
                        ReturnUrl = ReturnUrl,
                        ReturnUrlHash = ReturnUrlHash
                    }
                );
            }
    
            var userLoginAlreadyExists = user.Logins.Any(x =>
                x.TenantId == user.TenantId &&
                x.LoginProvider == externalLoginInfo.LoginProvider &&
                x.ProviderKey == externalLoginInfo.ProviderKey);
    
            if (!userLoginAlreadyExists)
            {
                user.AddLogin(new UserLoginInfo(
                        externalLoginInfo.LoginProvider,
                        externalLoginInfo.ProviderKey,
                        externalLoginInfo.ProviderDisplayName
                    )
                );
    
                (await UserManager.UpdateAsync(user)).CheckErrors();
            }
    
            return user;
        }
    
        protected virtual async Task CheckSelfRegistrationAsync()
        {
            if (!await SettingProvider.IsTrueAsync(AccountSettingNames.IsSelfRegistrationEnabled) ||
                !await SettingProvider.IsTrueAsync(AccountSettingNames.EnableLocalLogin))
            {
                throw new UserFriendlyException(L["SelfRegistrationDisabledMessage"]);
            }
        }
    
        protected virtual async Task SetUseCaptchaAsync()
        {
            UseCaptcha = !IsExternalLogin && await SettingProvider.IsTrueAsync(AccountSettingNames.Captcha.UseCaptchaOnRegistration);
            if (UseCaptcha)
            {
                var reCaptchaVersion = await SettingProvider.GetAsync<int>(AccountSettingNames.Captcha.Version);
                await ReCaptchaOptions.SetAsync(reCaptchaVersion == 3 ? reCAPTCHAConsts.V3 : reCAPTCHAConsts.V2);
            }
        }
    
        protected virtual async Task StoreConfirmUser(IdentityUser user)
        {
            var identity = new ClaimsIdentity(ConfirmUserModel.ConfirmUserScheme);
            identity.AddClaim(new Claim(AbpClaimTypes.UserId, user.Id.ToString()));
            if (user.TenantId.HasValue)
            {
                identity.AddClaim(new Claim(AbpClaimTypes.TenantId, user.TenantId.ToString()));
            }
            await HttpContext.SignInAsync(ConfirmUserModel.ConfirmUserScheme, new ClaimsPrincipal(identity));
        }
    
        private async Task TrySetEmailAsync()
        {
            if (IsExternalLogin)
            {
                var externalLoginInfo = await SignInManager.GetExternalLoginInfoAsync();
                if (externalLoginInfo == null)
                {
                    return;
                }
    
                if (!externalLoginInfo.Principal.Identities.Any())
                {
                    return;
                }
    
                var identity = externalLoginInfo.Principal.Identities.First();
                var emailClaim = identity.FindFirst(ClaimTypes.Email);
    
                if (emailClaim == null)
                {
                    return;
                }
    
                Input = new PostInput { EmailAddress = emailClaim.Value };
            }
        }
    
        public class PostInput
        {
            [Required]
            [DynamicStringLength(typeof(IdentityUserConsts), nameof(IdentityUserConsts.MaxUserNameLength))]
            public string UserName { get; set; }
    
            [Required]
            [EmailAddress]
            [DynamicStringLength(typeof(IdentityUserConsts), nameof(IdentityUserConsts.MaxEmailLength))]
            public string EmailAddress { get; set; }
    
            [Required]
            [DynamicStringLength(typeof(IdentityUserConsts), nameof(IdentityUserConsts.MaxPasswordLength))]
            [DataType(DataType.Password)]
            [DisableAuditing]
            public string Password { get; set; }
    
        }
    
        public class Field
        {
            public int Index { get; set; }
            public string? Title { get; set; }
            public bool IsRequired { get; set; }
            public bool HasOtherOption { get; set; }
            public QuestionTypes QuestionType { get; set; }
            public string? Answer { get; set; }
            public List<string>? Answers { get; set; }
            public List<SelectListItem>? Choices { get; set; }
        }
    }
    

    Here the variable "Fields" holds the details of those extra input fields which I am fetching from the database. As you can see those fields would not appear after clicking on Register, can you suggest what can I do so that the fields won't disappear.

  • User Avatar
    0
    liangshiwei created
    Support Team Fullstack Developer

    Hi,

    Could you also share the cshtml code?

  • User Avatar
    0
    pvala created

    Yeah, here you go.

    @page @using Microsoft.AspNetCore.Mvc.Localization @using Volo.Abp.Account.Localization @using Volo.Abp.Account.Public.Web.Security.Recaptcha @using Volo.Abp.Account.Settings @using Volo.Abp.Settings @model G1.health.AuthServer.Pages.Account.RegisterModel @using Volo.Forms; @inject IHtmlLocalizer<AccountResource> L @inject Volo.Abp.AspNetCore.Mvc.UI.Layout.IPageLayout PageLayout @inject ISettingProvider SettingProvider @{ PageLayout.Content.Title = L["Register"].Value; var reCaptchaVersion = await SettingProvider.GetAsync<int>(AccountSettingNames.Captcha.Version); }

    @if (!Model.LocalLoginDisabled) { @section scripts { <abp-script-bundle name="@typeof(G1.health.AuthServer.Pages.Account.RegisterModel).FullName"> <abp-script src="/Pages/Account/Register.js" /> </abp-script-bundle>

    @if (Model.UseCaptcha)
        {
            if (reCaptchaVersion == 3)
            {
                &lt;recaptcha-script-v3 /&gt;
                &lt;recaptcha-script-v3-js action=&quot;register&quot; callback=&quot;(function(){$(&#39;#@RecaptchaValidatorBase.RecaptchaResponseKey&#39;).val(token)})&quot; /&gt;
            }
            else
            {
                &lt;recaptcha-script-v2 /&gt;
            }
        }
    }
    
    &lt;style&gt;
        .hidden-scroll::-webkit-scrollbar {
            display: none;
        }
    
        .hidden-scroll{
            height: 500px;
            overflow: scroll
        }
    &lt;/style&gt;
    
    &lt;div class=&quot;account-module-form hidden-scroll&quot;&gt;
        &lt;h5 class=&quot;mb-2&quot;&gt;@L["AlreadyRegistered"] &lt;a class=&quot;text-decoration-none&quot; href=&quot;@Url.Page(&quot;./Login&quot;, new {returnUrl = Model.ReturnUrl, returnUrlHash = Model.ReturnUrlHash})&quot;&gt;@L["Login"]&lt;/a&gt;&lt;/h5&gt;
        &lt;form method=&quot;post&quot;&gt;
    
    
            @if (Model.UseCaptcha)
            {
                &lt;div class=&quot;mb-2&quot;&gt;
                    &lt;input type=&quot;hidden&quot; name=&quot;@RecaptchaValidatorBase.RecaptchaResponseKey&quot; id=&quot;@RecaptchaValidatorBase.RecaptchaResponseKey&quot; /&gt;
                &lt;/div&gt;
            }
    
            @if (!Model.IsExternalLogin)
            {
                &lt;div class=&quot;form-floating mb-2&quot;&gt;
                    &lt;input asp-for=&quot;Input.UserName&quot; type=&quot;text&quot; class=&quot;form-control&quot; placeholder=&quot;name@example.com&quot; auto-focus=&quot;true&quot;&gt;
                    @Html.LabelFor(m => m.Input.UserName)
                &lt;/div&gt;
            }
    
            &lt;div class=&quot;form-floating mb-2&quot;&gt;
                &lt;input asp-for=&quot;Input.EmailAddress&quot; type=&quot;email&quot; class=&quot;form-control&quot; placeholder=&quot;name@example.com&quot; auto-focus=&quot;true&quot;&gt;
                @Html.LabelFor(m => m.Input.EmailAddress)
            &lt;/div&gt;
    
    
            @if (!Model.IsExternalLogin)
            {
                &lt;div class=&quot;form-floating mb-2&quot;&gt;
                    &lt;input asp-for=&quot;Input.Password&quot; id=&quot;password-input&quot; type=&quot;password&quot; class=&quot;form-control&quot; placeholder=&quot;Password&quot;&gt;
                    @Html.LabelFor(m => m.Input.Password)
                    &lt;i id=&quot;PasswordVisibilityButton&quot; class=&quot;bi bi-eye-slash show-pass-icon&quot; data-bs-toggle=&quot;tooltip&quot; data-bs-placement=&quot;top&quot; data-bs-html=&quot;true&quot; aria-label=&quot;@L[&quot;ShowPassword&quot;]&quot; data-bs-original-title=&quot;@L[&quot;ShowPassword&quot;]&quot;&gt;&lt;/i&gt;
                    &lt;i id=&quot;capslockicon&quot; class=&quot;bi bi-capslock caps-lock-icon&quot; style=&quot;display: none;&quot; data-bs-toggle=&quot;tooltip&quot; data-bs-placement=&quot;top&quot; data-bs-html=&quot;true&quot; aria-label=&quot;&lt;i class=&#39;bi bi-exclamation-circle&#39;&gt;&lt;/i&gt; @L["CapsLockOn"]!" data-bs-original-title="&lt;i class=&#39;bi bi-exclamation-circle&#39;&gt;&lt;/i&gt; @L["CapsLockOn"]!">&lt;/i&gt;
                &lt;/div&gt;
            }
    
            @for (int i = 0; i &lt; Model.Fields.Count; i++)
            {
                switch (Model.Fields[i].QuestionType)
                {
                    case QuestionTypes.ShortText:
                        &lt;div class=&quot;form-floating mb-2&quot;&gt;
                            &lt;input type=&quot;hidden&quot; asp-for=&quot;@Model.Fields[i].Index&quot; /&gt;
                            &lt;input type=&quot;hidden&quot; asp-for=&quot;@Model.Fields[i].Title&quot; /&gt;
                            &lt;input type=&quot;hidden&quot; asp-for=&quot;@Model.Fields[i].IsRequired&quot; /&gt;
                            &lt;input type=&quot;hidden&quot; asp-for=&quot;@Model.Fields[i].QuestionType&quot; /&gt;
                            &lt;input asp-for=&quot;@Model.Fields[i].Answer&quot; required=&quot;@Model.Fields[i].IsRequired&quot; type=&quot;text&quot; class=&quot;form-control&quot; auto-focus=&quot;true&quot;&gt;
                            @Html.Label(Model.Fields[i].Title)
                        &lt;/div&gt;
                        break;
    
                    case QuestionTypes.ParagraphText:
                        &lt;div class=&quot;form-floating mb-2&quot;&gt;
                            &lt;input type=&quot;hidden&quot; asp-for=&quot;@Model.Fields[i].Index&quot; /&gt;
                            &lt;input type=&quot;hidden&quot; asp-for=&quot;@Model.Fields[i].Title&quot; /&gt;
                            &lt;input type=&quot;hidden&quot; asp-for=&quot;@Model.Fields[i].IsRequired&quot; /&gt;
                            &lt;input type=&quot;hidden&quot; asp-for=&quot;@Model.Fields[i].QuestionType&quot; /&gt;
                            &lt;input asp-for=&quot;@Model.Fields[i].Answer&quot; required=&quot;@Model.Fields[i].IsRequired&quot; type=&quot;text&quot; class=&quot;form-control&quot; auto-focus=&quot;true&quot;&gt;
                            @Html.Label(Model.Fields[i].Title)
                        &lt;/div&gt;
                        break;
    
                    case QuestionTypes.ChoiceMultiple:
                        &lt;div class=&quot;form-floating mb-2&quot;&gt;
                            &lt;input type=&quot;hidden&quot; asp-for=&quot;@Model.Fields[i].Index&quot; /&gt;
                            &lt;input type=&quot;hidden&quot; asp-for=&quot;@Model.Fields[i].Title&quot; /&gt;
                            &lt;input type=&quot;hidden&quot; asp-for=&quot;@Model.Fields[i].IsRequired&quot; /&gt;
                            &lt;input type=&quot;hidden&quot; asp-for=&quot;@Model.Fields[i].HasOtherOption&quot; /&gt;
                            &lt;input type=&quot;hidden&quot; asp-for=&quot;@Model.Fields[i].QuestionType&quot; /&gt;
                            &lt;input type=&quot;hidden&quot; asp-for=&quot;@Model.Fields[i].Choices&quot; /&gt;
                            &lt;lable&gt;@Model.Fields[i].Title&lt;/lable&gt;
                            &lt;abp-radio asp-for=&quot;@Model.Fields[i].Answer&quot; required=&quot;@Model.Fields[i].IsRequired&quot; asp-items=&quot;@Model.Fields[i].Choices&quot; /&gt;
                        &lt;/div&gt;
                        break;
    
                    case QuestionTypes.Checkbox:
                        &lt;div class=&quot;form-floating mb-2&quot;&gt;
                            &lt;input type=&quot;hidden&quot; asp-for=&quot;@Model.Fields[i].Index&quot; /&gt;
                            &lt;input type=&quot;hidden&quot; asp-for=&quot;@Model.Fields[i].Title&quot; /&gt;
                            &lt;input type=&quot;hidden&quot; asp-for=&quot;@Model.Fields[i].IsRequired&quot; /&gt;
                            &lt;input type=&quot;hidden&quot; asp-for=&quot;@Model.Fields[i].HasOtherOption&quot; /&gt;
                            &lt;input type=&quot;hidden&quot; asp-for=&quot;@Model.Fields[i].QuestionType&quot; /&gt;
                            &lt;input type=&quot;hidden&quot; asp-for=&quot;@Model.Fields[i].Choices&quot; /&gt;
                            @foreach (var choice in Model.Fields[i].Choices)
                            {
                                &lt;abp-input asp-for=&quot;@Model.Fields[i].Answer&quot; required=&quot;@Model.Fields[i].IsRequired&quot; label=&quot;@choice.Value&quot; type=&quot;checkbox&quot; /&gt;
                            }
                        &lt;/div&gt;
                        break;
    
                    case QuestionTypes.DropdownList:
                        &lt;div class=&quot;form-floating mb-2&quot;&gt;
                            &lt;input type=&quot;hidden&quot; asp-for=&quot;@Model.Fields[i].Index&quot; /&gt;
                            &lt;input type=&quot;hidden&quot; asp-for=&quot;@Model.Fields[i].Title&quot; /&gt;
                            &lt;input type=&quot;hidden&quot; asp-for=&quot;@Model.Fields[i].IsRequired&quot; /&gt;
                            &lt;input type=&quot;hidden&quot; asp-for=&quot;@Model.Fields[i].HasOtherOption&quot; /&gt;
                            &lt;input type=&quot;hidden&quot; asp-for=&quot;@Model.Fields[i].QuestionType&quot; /&gt;
                            &lt;input type=&quot;hidden&quot; asp-for=&quot;@Model.Fields[i].Choices&quot; /&gt;
                            &lt;abp-select asp-for=&quot;@Model.Fields[i].Answer&quot; required=&quot;@Model.Fields[i].IsRequired&quot; label=&quot;@Model.Fields[i].Title&quot; asp-items=&quot;@Model.Fields[i].Choices&quot; /&gt;
                        &lt;/div&gt;
                        break;
    
                    case QuestionTypes.MultipleSelect:
                        &lt;div class=&quot;form-floating mb-2&quot;&gt;
                            &lt;input type=&quot;hidden&quot; asp-for=&quot;@Model.Fields[i].Index&quot; /&gt;
                            &lt;input type=&quot;hidden&quot; asp-for=&quot;@Model.Fields[i].Title&quot; /&gt;
                            &lt;input type=&quot;hidden&quot; asp-for=&quot;@Model.Fields[i].IsRequired&quot; /&gt;
                            &lt;input type=&quot;hidden&quot; asp-for=&quot;@Model.Fields[i].HasOtherOption&quot; /&gt;
                            &lt;input type=&quot;hidden&quot; asp-for=&quot;@Model.Fields[i].QuestionType&quot; /&gt;
                            &lt;input type=&quot;hidden&quot; asp-for=&quot;@Model.Fields[i].Choices&quot; /&gt;
                            &lt;abp-select asp-for=&quot;@Model.Fields[i].Answers&quot; required=&quot;@Model.Fields[i].IsRequired&quot; label=&quot;@Model.Fields[i].Title&quot; asp-items=&quot;@Model.Fields[i].Choices&quot; /&gt;
                        &lt;/div&gt;
                        break;
    
                    case QuestionTypes.Calendar:
                        &lt;div class=&quot;form-floating mb-2&quot;&gt;
                            &lt;input type=&quot;hidden&quot; asp-for=&quot;@Model.Fields[i].Index&quot; /&gt;
                            &lt;input type=&quot;hidden&quot; asp-for=&quot;@Model.Fields[i].Title&quot; /&gt;
                            &lt;input type=&quot;hidden&quot; asp-for=&quot;@Model.Fields[i].IsRequired&quot; /&gt;
                            &lt;input type=&quot;hidden&quot; asp-for=&quot;@Model.Fields[i].QuestionType&quot; /&gt;
                            &lt;input asp-for=&quot;@Model.Fields[i].Answer&quot; required=&quot;@Model.Fields[i].IsRequired&quot; type=&quot;datetime-local&quot; class=&quot;form-control&quot; auto-focus=&quot;true&quot; /&gt;
                            @Html.Label(Model.Fields[i].Title)
                        &lt;/div&gt;
                        break;
    
                    case QuestionTypes.Date:
                        &lt;div class=&quot;form-floating mb-2&quot;&gt;
                            &lt;input type=&quot;hidden&quot; asp-for=&quot;@Model.Fields[i].Index&quot; /&gt;
                            &lt;input type=&quot;hidden&quot; asp-for=&quot;@Model.Fields[i].Title&quot; /&gt;
                            &lt;input type=&quot;hidden&quot; asp-for=&quot;@Model.Fields[i].IsRequired&quot; /&gt;
                            &lt;input type=&quot;hidden&quot; asp-for=&quot;@Model.Fields[i].QuestionType&quot; /&gt;
                            &lt;input asp-for=&quot;@Model.Fields[i].Answer&quot; required=&quot;@Model.Fields[i].IsRequired&quot; type=&quot;date&quot; class=&quot;form-control&quot; auto-focus=&quot;true&quot;&gt;
                            @Html.Label(Model.Fields[i].Title)
                        &lt;/div&gt;
                        break;
    
                    case QuestionTypes.Time:
                        &lt;div class=&quot;form-floating mb-2&quot;&gt;
                            &lt;input type=&quot;hidden&quot; asp-for=&quot;@Model.Fields[i].Index&quot; /&gt;
                            &lt;input type=&quot;hidden&quot; asp-for=&quot;@Model.Fields[i].Title&quot; /&gt;
                            &lt;input type=&quot;hidden&quot; asp-for=&quot;@Model.Fields[i].IsRequired&quot; /&gt;
                            &lt;input type=&quot;hidden&quot; asp-for=&quot;@Model.Fields[i].QuestionType&quot; /&gt;
                            &lt;input asp-for=&quot;@Model.Fields[i].Answer&quot; required=&quot;@Model.Fields[i].IsRequired&quot; type=&quot;time&quot; class=&quot;form-control&quot; auto-focus=&quot;true&quot;&gt;
                            @Html.Label(Model.Fields[i].Title)
                        &lt;/div&gt;
                        break;
                }
            }
    
            @if (reCaptchaVersion == 2)
            {
                &lt;recaptcha-div-v2 callback=&quot;(function(){$(&#39;#@RecaptchaValidatorBase.RecaptchaResponseKey&#39;).val(token)})&quot; /&gt;
            }
    
            &lt;div class=&quot;d-grid gap-2&quot;&gt;
                &lt;abp-button button-type=&quot;Primary&quot; type=&quot;submit&quot; class=&quot;mt-2 mb-3&quot;&gt;@L["Register"]&lt;/abp-button&gt;
            &lt;/div&gt;
        &lt;/form&gt;
    &lt;/div&gt;
    

    }

  • User Avatar
    0
    pvala created

    If you run this, now you will see that it works and it will hold the values, but here I have done a work around. I have created a separate input field for all the properties and I am keeping them hidden for now, because that's the only way I could find in order to hold the values. I'd like something more proper to do that.

    For example :

                            &lt;input type=&quot;hidden&quot; asp-for=&quot;@Model.Fields[i].Index&quot; /&gt;
                            &lt;input type=&quot;hidden&quot; asp-for=&quot;@Model.Fields[i].Title&quot; /&gt;
                            &lt;input type=&quot;hidden&quot; asp-for=&quot;@Model.Fields[i].IsRequired&quot; /&gt;
                            &lt;input type=&quot;hidden&quot; asp-for=&quot;@Model.Fields[i].HasOtherOption&quot; /&gt;
                            &lt;input type=&quot;hidden&quot; asp-for=&quot;@Model.Fields[i].QuestionType&quot; /&gt;
                            &lt;input type=&quot;hidden&quot; asp-for=&quot;@Model.Fields[i].Choices&quot; /&gt;
    
  • User Avatar
    0
    liangshiwei created
    Support Team Fullstack Developer

    Hi, You can try update the CreateExtraFields method

    private async Task CreateExtraFields()
    {
        ....
    
        Fields ??= new List<Field>();
    
        foreach (var item in QuestionDtos)
        {
            var choices = new List<SelectListItem>();
    
            foreach (var choice in item.Choices)
            {
                choices.Add(new SelectListItem()
                {
                    Text = choice.Value,
                    Value = choice.Value
                });
            }
    
            Field field = Fields.FirstOrDefault(x => x.Title == item.Title) ?? new Field();
            field.Index = item.Index;
    
            field.Index = item.Index;
            field.Title = item.Title;
            field.IsRequired = item.IsRequired;
            field.HasOtherOption = item.HasOtherOption;
            field.QuestionType = item.QuestionType;
            field.Choices = choices;
    
            Fields.AddIfNotContains(x => x.Title == field.Title ,() =>field);
        }
    }
    

    And OnPostAsync method

    public virtual async Task<IActionResult> OnPostAsync()
    {
        try
        {
            await CheckSelfRegistrationAsync();
            await SetUseCaptchaAsync();
            await CreateExtraFields(); add this line
            
            ....
        }
      
       ....
    }   
    

    The cshtml

    ...
    case QuestionTypes.ShortText:
        <div class="form-floating mb-2">
            <input type="hidden" asp-for="@Model.Fields[i].Title" /> // keep this line
            <input asp-for="@Model.Fields[i].Answer" required="@Model.Fields[i].IsRequired" type="text" class="form-control" auto-focus="true">
            @Html.Label(Model.Fields[i].Title)
        </div>
        break;
    
    case QuestionTypes.ParagraphText:
        <div class="form-floating mb-2">
            <input type="hidden" asp-for="@Model.Fields[i].Title" /> // keep this line
            <input asp-for="@Model.Fields[i].Answer" required="@Model.Fields[i].IsRequired" type="text" class="form-control" auto-focus="true">
            @Html.Label(Model.Fields[i].Title)
        </div>
        break;
    ....
    
  • User Avatar
    0
    pvala created

    This will work, but it's basically calling the API again to get the details of the input fields, and that doesn't sound great. Can you give me something else? Something which can hold the values of the variable till the page is refreshed or the instance of the class is destroyed.

  • User Avatar
    0
    liangshiwei created
    Support Team Fullstack Developer

    Hi,

    Something which can hold the values of the variable till the page is refreshed or the instance of the class is destroyed.

    The server is stateless, you must re-call the app service or temporarily store the value. you can consider using Session to store values.

    https://learn.microsoft.com/en-us/aspnet/core/fundamentals/app-state?view=aspnetcore-8.0 https://learn.microsoft.com/en-us/aspnet/core/fundamentals/app-state?view=aspnetcore-8.0#set-and-get-session-values

Made with ❤️ on ABP v9.1.0-preview. Updated on December 05, 2024, 12:19