MVC必需属性不能在客户端一致地工作

时间:2016-04-26 19:41:05

标签: asp.net-mvc data-annotations

每次清除字段并单击另一个字段时,我都无法显示所需的字段验证消息。它会在我清除多个字段并且首先发生另一种类型的验证消息后出现,但不是在我刚进入表单时清除所需字段,然后单击另一个字段。下面是我在BundleConfigs中的RegisterBundles方法,它包含了我认为是jqueryval的正确包。

     public static void RegisterBundles(BundleCollection bundles)
    {

        //other bundles...

        bundles.Add(new ScriptBundle("~/bundles/jqueryval").Include("~/Scripts/jquery.unobtrusive*",
                    "~/Scripts/jquery.validate*"));

        //other bundles ...

    }

这是我的_Layout.cshtml文件的样子,似乎我已经渲染了所有的脚本包。

  <!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    @Styles.Render("~/Content/css")
    @Scripts.Render("~/bundles/jquery")
    @Scripts.Render("~/bundles/bootstrap")
    @Scripts.Render("~/bundles/modernizr")
    @Scripts.Render("~/bundles/xeditable")
    @Scripts.Render("~/bundles/bootgrid")

</head>
<body>

    @*Other layout stuff...*@


    @Scripts.Render("~/bundles/jqueryval")
    @RenderSection("scripts", required: false)


</body>

</html>

根据我研究过的内容,这里的Model类似乎是正确的。

public class ModelClass
    {
        //fields...

        public Decimal DecimalProperty{get; set;}

        [Required(ErrorMessage="REQUIRED!")]
        public DateTime DateProperty { get; set; }


        [Required(ErrorMessage="REQUIRED!")]
        public DateTime OtherDateProperty { get; set; }

         //Other properties not associated with visible form fields..
    }

最后,我的视图中有标签,文本框和验证消息组件。

    @model ModelClass


        @using (Html.BeginForm("Action", "Controller", new { id = Model.ID}, FormMethod.Post, new { @class = "form-horizontal", role = "form" }))
    {
        @Html.AntiForgeryToken()
        @Html.ValidationSummary(true, "", new { @class = "text-danger" })

        <div class="form-group">
            @Html.LabelFor(m => m.DecimalProperty, new { @class = "col-md-2 control-label" })
            <div class="col-md-10">
                @Html.TextBoxFor(m => m.DecimalProperty, new { @class = "form-control", @type = "text" })
                @Html.ValidationMessageFor(m => m.DecimalProperty, "", new { @class = "text-danger" })
            </div>
        </div>
        <div class="form-group">
            @Html.LabelFor(m => m.DateProperty, new { @class = "col-md-2 control-label" })
            <div class="col-md-10">
                @Html.TextBoxFor(m => m.DateProperty, new { @class = "form-control", @type = "date" })
                @Html.ValidationMessageFor(m => m.DateProperty, "", new { @class = "text-danger" })
            </div>
        </div>
        <div class="form-group">
                @Html.LabelFor(m => m.OtherDateProperty, "Other Date", new { @class = "col-md-2 control-label" })
                <div class="col-md-10">
                    @Html.TextBoxFor(m => m.OtherDateProperty, new { @class = "form-control", @type = "date"})
                    @Html.ValidationMessageFor(m => m.OtherDateProperty, "", new { @class = "text-danger" })
                </div>
        </div>
        <div class="form-group">
            <div class="col-md-offset-2 col-md-10">
                <input type="submit" value="Save" class="btn btn-primary btn-md" />
            </div>
        </div>
    }
</div>

请注意我在MVC方面有点新鲜,我试图拿出一些我认为不需要理解的内容。

1 个答案:

答案 0 :(得分:0)

评论部分无法取得一切。这是你想念的:

    [Required(ErrorMessage="REQUIRED!")]
    public Decimal DecimalProperty{get; set;}

    [Required(ErrorMessage="REQUIRED!")]
    public DateTime DateProperty { get; set; }


    [Required(ErrorMessage="REQUIRED!")]
    public DateTime OtherDateProperty { get; set; }