在提交时验证(MVC)时避免回发

时间:2016-12-19 18:35:17

标签: c# jquery asp.net-mvc

按“提交”时,如何避免回复验证?当按下提交时,页面重新加载/回发,然后它给我验证错误。

@model X.Models.ModelVm

@using (Html.BeginForm())
{
    @Html.AntiForgeryToken()

    <div class="form-horizontal">

        @Html.ValidationSummary(true, "", new { @class = "text-danger" })
        <div class="form-group">
            @Html.LabelFor(model => model.Birthday, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.Birthday, new { htmlAttributes = new { @class = "form-control" } })  
                @Html.ValidationMessageFor(model => model.Birthday, "", new { @class = "text-danger" })             
            </div>
        </div>

        .....

        <div class="form-group">
            <div class="col-md-offset-2 col-md-10">
                <input type="submit" value="Get data" class="btn btn-default"/>                   
            </div>
        </div>
    </div>
}

我的ViewModel:

[Required]
[DataType(DataType.DateTime)]
public DateTime Birthday { get; set; }

1 个答案:

答案 0 :(得分:2)

  1. 开启<AL/alext.h>ClientValidationEnabled
  2. 在您的视图中加入UnobtrusiveJavaScriptEnabledjqueryjquery.validate.js