验证所需的 MVC3 hidefor() 字段

发布于 2024-12-17 23:43:13 字数 1523 浏览 1 评论 0原文

我正在尝试验证从外部 jQuery 调用更新的隐藏字段。

模型:

    [Required(ErrorMessage = "UserId is required")]
    public int? UserId { get; set; }

母版页:

    <script type="text/javascript" src='<%= ResolveUrl("~/Scripts/jquery-1.7.min.js") %>'></script>
    <script type="text/javascript" src='<%= ResolveUrl("~/Scripts/jquery-ui-1.8.16.custom.min.js") %>'></script>
    <script type="text/javascript" src='<%= ResolveUrl("~/Scripts/jquery.numeric.js") %>'></script>
    <script type="text/javascript" src='<%= ResolveUrl("~/Scripts/jquery.validate.min.js") %>'></script>
    <script type="text/javascript" src='<%= ResolveUrl("~/Scripts/jquery.validate.unobtrusive.min.js") %>'></script>
    <script type="text/javascript" src='<%= ResolveUrl("~/Scripts/RangeDateValidator.js") %>'></script>
    <asp:ContentPlaceHolder ID="MainContent" runat="server" />

视图:

    <asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
    <% using (Html.BeginForm("FormSave", "Form", FormMethod.Post))
       {%>
    <%: Html.ValidationMessageFor(model => model.UserId) %>
    <%: Html.HiddenFor(model => model.UserId) %>
    <p>
        <input type="submit" value="Save" />
    </p>
    <% } %>
    </asp:Content>

表单未成功保存,表明已成功需要该属性,但错误消息从未在属性的验证消息中显示

I'm trying to validate a hidden field that is updated from an external jQuery call.

Model:

    [Required(ErrorMessage = "UserId is required")]
    public int? UserId { get; set; }

Master Page:

    <script type="text/javascript" src='<%= ResolveUrl("~/Scripts/jquery-1.7.min.js") %>'></script>
    <script type="text/javascript" src='<%= ResolveUrl("~/Scripts/jquery-ui-1.8.16.custom.min.js") %>'></script>
    <script type="text/javascript" src='<%= ResolveUrl("~/Scripts/jquery.numeric.js") %>'></script>
    <script type="text/javascript" src='<%= ResolveUrl("~/Scripts/jquery.validate.min.js") %>'></script>
    <script type="text/javascript" src='<%= ResolveUrl("~/Scripts/jquery.validate.unobtrusive.min.js") %>'></script>
    <script type="text/javascript" src='<%= ResolveUrl("~/Scripts/RangeDateValidator.js") %>'></script>
    <asp:ContentPlaceHolder ID="MainContent" runat="server" />

View:

    <asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
    <% using (Html.BeginForm("FormSave", "Form", FormMethod.Post))
       {%>
    <%: Html.ValidationMessageFor(model => model.UserId) %>
    <%: Html.HiddenFor(model => model.UserId) %>
    <p>
        <input type="submit" value="Save" />
    </p>
    <% } %>
    </asp:Content>

The form does not save successfully, indicating that the attribute is being successfully required but the error message never shows in the validationmessage for attribute

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文