How Can I Prevent My Form from Clearing Fields on Submit in Asp.NET Razor Pages?

58 Views Asked by At

This is a register page the if sign-up was success, user will redirect to index page and if sign-up fail the page shows relating errors but all fields become clear, how can I prevent this ?

   <form method="post" asp-page-handler="Register" id="myForm">
      <div asp-validation-summary="All" class="text-danger"> </div>

      <div class="edit-profile__body">
          <div class="form-group mb-20">
              <label for="name">User name</label>
              <input type="text" class="form-control" name="UserName" id="name" placeholder="user name">
          </div>
          <div class="form-group mb-20">
              <label for="username">showing name</label>
              <input type="text" class="form-control" id="username" name="ShowName" placeholder="showing name">
          </div>
          <div class="form-group mb-20">
              <label for="email">Email</label>
              <input type="text" class="form-control" name="Email" id="email" placeholder="[email protected]">
          </div>
          <div class="form-group mb-15">
              <label for="password-field">password</label>
              <span asp-validation="password"></span>
              <div class="position-relative">
                  <input id="password-field" type="password" class="form-control" name="password"
                         placeholder="password">
                  <div class="uil uil-eye-slash text-lighten fs-15 field-icon toggle-password2"></div>
              </div>
          </div>
          <div class="admin-condition">
              <div class="checkbox-theme-default custom-checkbox">
                  <input class="checkbox" type="checkbox" name="ConfirmRules" id="admin-1" value="true">
                  <label for="admin-1">
                      <span class="checkbox-text">
                          I accept <a href="#" class="color-primary">Rules</a>
                          
                      </span>
                  </label>
              </div>
          </div>
          <div class="admin__button-group button-group d-flex pt-1 justify-content-md-start justify-content-center">
              <button type="submit" id="formSubmit" class="btn btn-primary btn-default w-100 btn-squared text-capitalize lh-normal px-50 signIn-createBtn ">
                  Create Account
              </button>
          </div>
      </div>
  </form>
public async Task<IActionResult> OnPostRegister(SignUpVM signUpVM, CancellationToken cancellationToken)
{
    UserManagerResponse result;

    if (ModelState.IsValid && signUpVM.ConfirmRules)
    {
        var model = new UserDTO()
        {
            UserName = signUpVM.UserName,
            ShowName = signUpVM.ShowName,
            Email = signUpVM.Email,
            Password = signUpVM.Password,
        };

        result = await _userAppServices.Register(model, cancellationToken);

        if (result.IsSuccess)
        {
            return LocalRedirect("Index");
        }
        else
        {
            ModelState.AddModelError(string.Empty, result.Message);

            return default;
        }
    }
    else
    {
        return default;
    }
}

also my OnGet method is empty.

I tried using js preventDefault() but it doesn't work . because it prevent my form from posting.

0

There are 0 best solutions below