无法使用不显眼的js验证表单

时间:2019-02-19 05:57:53

标签: javascript asp.net-mvc c#-4.0 unobtrusive-validation

我有一个简单的注册表格,但无法启动客户端验证

   @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文件也都已渲染。

即使我已经在web.config中启用了clientsidevalidation和UnobtrusiveJavaScriptEnabled。

即使从控制器端(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);
                },

            });
        }
    }

0 个答案:

没有答案