asp.net mvc 2项目不渲染数据注释属性

发布于 2024-12-07 15:01:00 字数 1566 浏览 0 评论 0原文

数据注释属性未渲染会出现什么问题?

web.config

<appSettings>
    <add key="ClientValidationEnabled" value="true" />
    <add key="UnobtrusiveJavaScriptEnabled" value="true" />
</appSettings>

C#

public class SearchCriteria
{
    [Required]
    public string ControlNo { get; set; }

    [Required]
    public string Insured { get; set; }

    [Required]
    public string PolicyNumber { get; set; }
}

ascx

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<Core.SearchCriteria>" %>
<%@ Import Namespace="Core" %>

    <% using (Html.BeginForm()) {%>
        <%= Html.ValidationSummary(true) %>

        <fieldset>
            <legend>Fields</legend>

            <div class="editor-label">
                <%= Html.LabelFor(model => model.ControlNo) %>
            </div>
            <div class="editor-field">
                <%= Html.TextBoxFor(model => model.ControlNo) %>
                <%= Html.ValidationMessageFor(model => model.ControlNo) %>
            </div>

            <div class="editor-label">
                <%= Html.LabelFor(model => model.Insured) %>
            </div>
            <div class="editor-field">
                <%= Html.TextBoxFor(model => model.Insured) %>
                <%= Html.ValidationMessageFor(model => model.Insured) %>
            </div>

what can a problem that data annotation attributes are not rendered?

web.config

<appSettings>
    <add key="ClientValidationEnabled" value="true" />
    <add key="UnobtrusiveJavaScriptEnabled" value="true" />
</appSettings>

C#

public class SearchCriteria
{
    [Required]
    public string ControlNo { get; set; }

    [Required]
    public string Insured { get; set; }

    [Required]
    public string PolicyNumber { get; set; }
}

ascx

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<Core.SearchCriteria>" %>
<%@ Import Namespace="Core" %>

    <% using (Html.BeginForm()) {%>
        <%= Html.ValidationSummary(true) %>

        <fieldset>
            <legend>Fields</legend>

            <div class="editor-label">
                <%= Html.LabelFor(model => model.ControlNo) %>
            </div>
            <div class="editor-field">
                <%= Html.TextBoxFor(model => model.ControlNo) %>
                <%= Html.ValidationMessageFor(model => model.ControlNo) %>
            </div>

            <div class="editor-label">
                <%= Html.LabelFor(model => model.Insured) %>
            </div>
            <div class="editor-field">
                <%= Html.TextBoxFor(model => model.Insured) %>
                <%= Html.ValidationMessageFor(model => model.Insured) %>
            </div>

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

萌辣 2024-12-14 15:01:00

验证的问题是 MVC 2 不渲染数据注释属性!

相反,MVC 2 创建定义所有验证规则的 JS 对象,然后 MicrosoftMvcValidation.js 使用它。

the problem with validation was that MVC 2 does not render data annotation attributes !!!

instead MVC 2 creates JS object that defines all validation rules and then MicrosoftMvcValidation.js works with it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文