ASP.net MVC不引人注目的验证

时间:2018-05-08 21:17:30

标签: asp.net-mvc unobtrusive-validation

在将输入发送到Azure Logic App之前,我无法验证表单。单击视图中的“提交”按钮后,将提交表单内容而不进行验证。验证确实触发,但提交表单是否有效。有人可以帮忙吗?

...模型

     ans <- function(n) {
     x <- c(1,1)
     while (length(x) <= n) {
     position <- length(x)
     new <- x[position] + x[position-1]
     x <- c(x,new)
     }
     return(x[x<=n])
     }

查看...

    public class Contact
{
    [DisplayName("Name:")]
    [Required(ErrorMessage = "Please enter your name.")]
    public string Name { get; set; }

    [DisplayName("Email:")]
    [Required(ErrorMessage = "Please enter your email address.")]
    [RegularExpression(@"^\S+@\S+\.\S+$", ErrorMessage = "Please enter a valid email address.")]
    public string Email { get; set; }

    [DisplayName("Phone:")]
    [MaxLength(15)]
    [MinLength(8)]
    [RegularExpression(@"^[0-9]*$", ErrorMessage = "Phone number can only contain numbers.")]
    public string Phone { get; set; }

    [DisplayName("Message:")]
    [Required(ErrorMessage = "Please enter a message.")]
    public string Message { get; set; }

    [DisplayName("newsletterTextbox")]
    [Required(ErrorMessage = "Please enter your email address.")]
    [RegularExpression(@"^\S+@\S+\.\S+$", ErrorMessage = "Please enter a valid email address.")]
    public string newsletterTextbox { get; set; }
}

... JS

<div class="col-md-8 col-md-push-1">
            <!-- CONTACT FORM -->
            <div id="contactDiv" class="contact-form">
                <form id="contactForm">
                    <div class="form-group">
                        @Html.TextBoxFor(x => x.Name, new { placeholder = "Name", @class = "form-control" })
                        @Html.ValidationMessageFor(x => x.Name, "", new { @style = "color: Orange" })
                    </div>
                    <div class="form-group">
                        @Html.TextBoxFor(x => x.Email, new { placeholder = "Email", @class = "form-control" })
                        @Html.ValidationMessageFor(x => x.Email, "", new { @style = "color: Orange" })
                    </div>
                    <div class="form-group">
                        @Html.TextBoxFor(x => x.Phone, new { placeholder = "Phone", @class = "form-control" })
                    </div>
                    <div class="form-group">
                        @Html.TextBoxFor(x => x.Message, new { placeholder = "Message", @class = "form-control" })
                        @Html.ValidationMessageFor(x => x.Message, "", new { @style = "color: Orange" })
                    </div>
                    <button type="submit" id="submit" onclick="contactSubmit()" name="submit" class="btn btn-primary btn-lg text-center float-right">Submit your message</button>
                </form>
            </div>
            <!-- / CONTACT FORM -->
        </div>

1 个答案:

答案 0 :(得分:0)

包裹表单
@using (Ajax.BeginForm("ActionName", new { Controller = "ControllerName", area = "" }, new AjaxOptions() { OnSuccess = "onSuccessFunction", HttpMethod = "POST", UpdateTargetId = "idtobeupdated"}, new { id = "idofform" }))

并且不要使用javascript / jquery。

您需要为此安装Microsoft.jQuery.Unobtrusive.Validation和* .Ajax。