Невозможно проверить форму, используя ненавязчивый JS - PullRequest
0 голосов
/ 19 февраля 2019

У меня есть простая форма для регистрации, но я не могу запустить проверку на стороне клиента

   @using (Html.BeginForm(null, null, FormMethod.Post, new { id = "register_form" }))
        {
            <div class="row">
                <div class="col-sm-6 col xs-12">
                    <div class="form-group">
                        <label>First Name</label>
                        @Html.TextBoxFor(x => x.FirstName, new { @class = "form-control text-input", placeholder = Html.DisplayNameFor(x => x.FirstName) })
                        @Html.ValidationMessageFor(x => x.FirstName)
                    </div>
                    <div class="form-group">
                        <label>Email</label>
                        @Html.TextBoxFor(x => x.Email, new { @class = "form-control text-input", placeholder = Html.DisplayNameFor(x => x.Email) })
                        @Html.ValidationMessageFor(x => x.Email)

                    </div>
                    <div class="form-group">
                        <label>Confirm Password</label>
                        @Html.TextBoxFor(x => x.ConfirmPassword, new { @class = "form-control text-input", placeholder = Html.DisplayNameFor(x => x.ConfirmPassword), type = "password" })
                      @Html.ValidationMessageFor(x => x.ConfirmPassword)
                    </div>
                </div>

                <div class="col-sm-6 col xs-12">
                    <div class="form-group">
                        <label>Last Name</label>
                        @Html.TextBoxFor(x => x.LastName, new { @class = "form-control text-input", placeholder = Html.DisplayNameFor(x => x.LastName) })
                        @Html.ValidationMessageFor(x => x.LastName)
                    </div>
                    <div class="form-group">
                        <label>Password</label>
                        @Html.TextBoxFor(x => x.Password, new { @class = "form-control text-input", placeholder = Html.DisplayNameFor(x => x.Password), type = "password" })
                        @Html.ValidationMessageFor(x => x.Password)
                    </div>
                </div>
            </div>
            <hr />
            <div class="row">
                <div class="col-sm-6 col-xs-12">
                    <label class="checkbox-inline terms-text">
                        <input type="checkbox" value="">I Agree with Terms &
                        Condition
                    </label>
                </div>
                <div class="col-lg-6 col-md-6 col-sm-6 col-xs-12 btn-div">
                    <button type="submit" class="btn btn-default btn-submit" onclick="Signup()">Register</button>
                    <button type="clear" class="btn btn-default btn-submit" onclick="return clearForm()">
                        Reset
                    </button>
                </div>
            </div>

        }

enter image description here

Я вижу все файлы js такжеrender.

Даже если я включил проверку клиентов и UnobtrusiveJavaScriptEnabled в web.config.

Даже со стороны контроллера (ServerSide) не удается получить ошибки.

 [HttpPost]
    [ActionName("Register")]
    [AllowAnonymous]
    public ActionResult Register(User model)
    {
        if (ModelState.IsValid)
        {
            try
            {
                UserRepository contex = new UserRepository();
                contex.Register(model);

                return RedirectToAction("Index", "Home");
            }
            catch (MembershipCreateUserException e)
            {
                ModelState.AddModelError("", ErrorCodeToString(e.StatusCode));
            }
        }

        else
        {
            return View("SignUp", model);
        }
        // If we got this far, something failed, redisplay form
        return View(model);
    }

Я действительноЯ не понял, чего мне не хватает.

Класс пользователя

public sealed class User
 {
     #region Properties

     /// <summary>
     /// Gets or sets the UserID value.
     /// </summary>
     [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
     public int UserId { get; set; }

     /// <summary>
     /// Gets or sets the EmailAddress value.
     /// </summary>
     [Required(ErrorMessage = "Email Address required")]
     [RegularExpression(@"^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}" +
                      @"\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\" +
                      @".)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$",
                      ErrorMessage = "Email is not valid")]
     [Display(Name = "Email")]
     [StringLength(50, ErrorMessage = "Email address ")]
     public string Email { get; set; }

     /// <summary>
     /// Gets or sets the Password value.
     /// </summary>
     [Required(ErrorMessage = "Password required")]
     [StringLength(50, ErrorMessage = "*")]
     public string Password { get; set; }

     /// <summary>
     /// Gets or sets the Confirm Password value.
     /// </summary>
     [Required]
     [StringLength(50, ErrorMessage = "*")]
     [Compare("Password")]
     public string ConfirmPassword { get; set; }

     /// <summary>
     ///  Gets or sets of FirstName
     /// </summary>
     [Required]
     public string FirstName { get; set; }

     /// <summary>
     /// Gets or sets of LastName
     /// </summary>
     [Required]
     public string LastName { get; set; }

     /// <summary>
     /// Gets or sets the IsActive value.
     /// </summary>
     public bool? IsActive { get; set; }


     #endregion
 }

Функция регистрации

function Signup() {
        var $form = $('#register_form');
        var result =$form.valid();
        if (result) {

            debugger
            $.ajax({
                type: "POST",
                url: '/Account/Register',
                data: $form.serialize(),
                success: function (data) {

                },
                error: function (jqXHR, exception) {
                    AjaxCallError(jqXHR, exception);
                },

            });
        }
    }
...