ValidationMessageFor的问题

时间:2011-01-25 18:59:22

标签: asp.net css asp.net-mvc validation

您好,

我已将以下代码放在我的页面上:

<%: Html.ValidationSummary("Form not correct", new { @class = "errList" })%>

然后在每个属性上我都有类似的东西:

<%: Html.ValidationMessageFor(model => model.ModelViewAd.Title, "*", new { @class = "val" })%>

问题是,只要我打开页面,每个ValidationMessageFor都会显示*,即使表单尚未验证,ValidationSummary也会显示“表单不正确”?但是,在验证表单后,将首先显示摘要列表。

我需要ValidationMessageFor和ValidationSummary仅在表单经过ben验证时显示。

我错过了什么?

Edit1:属性确实有DataAnnotations。

EDIT2:

这是视图的第一部分的样子

<div id="adRegistrationForm">
            <% Html.EnableClientValidation(); %>
            <h1>Registrera din annons</h1>
            <%: Html.ValidationMessageFor(model => model.ModelViewAd, "*", new { @class = "val" })%>
            <br />
            <% using (Html.BeginForm("Register", "Ad", FormMethod.Post, new { enctype = "multipart/form-data" }))
               {%>
                <%: Html.ValidationSummary("Formuläret är inte korrekt ifyllt", new { @class = "errList" })%>

                <div class="controlTitle">
                    <%: Html.LabelFor(model => model.ModelViewAd.Title)%>
                    <%: Html.ValidationMessageFor(model => model.ModelViewAd.Title, "*", new { @class = "val" })%>
                </div>
                <div>
                    <%: Html.TextBoxFor(model => model.ModelViewAd.Title, new { @class = "tb1" })%>
                </div><br />

                <div class="controlTitle">
                    <%: Html.LabelFor(model => model.ModelViewAd.TypeOfAd)%>
                </div>
                <div>
                    <%: MvcHtmlString.Create(Html.RadioButtonListFor(model => model.ModelViewAd.TypeOfAd)) %>
                </div><br />

                <div id="divEndDate">
                    <div class="controlTitle">
                        <%: Html.LabelFor(model => model.ModelViewAd.EndDate)%>
                        <%: Html.ValidationMessageFor(model => model.ModelViewAd.EndDate, "*", new { @class = "val" })%>
                    </div>
                    <div>
                        <%: Html.TextBoxFor(model => model.ModelViewAd.EndDate)%>
                    </div>
                </div>

这就是viewClass的第一部分:

[PropertiesMustMatchAttribute("P1", "P2", ErrorMessage = "Lösenorden stämmer inte")]
    public class ModelViewAdRegister
    {
        #region Fields
        private string _title;
        private string _description;
        #endregion

        [Required(ErrorMessage = "Titel saknas")]
        [StringLength(50, MinimumLength = 5, ErrorMessage = "Titeln får vara mellan 5-50 tecken lång")]
        [DisplayName("Titel")]
        public string Title
        {
            get { return _title; }
            set { _title = value ?? string.Empty; }
        }

        [Required(ErrorMessage = "Pris saknas")]
        [DisplayName("Pris (skr)")]
        public Decimal Price { get; set; }

        [Required(ErrorMessage = "Annons text saknas")]
        [DisplayName("Annons text")]
        [StringLength(50000, MinimumLength = 10, ErrorMessage = "Annons texten får vara mellan 10-50000 tecken lång")]
        public string Description
        {
            get { return _description; }
            set { _description = value ?? string.Empty; }
        }

1 个答案:

答案 0 :(得分:1)

我已转载您的问题。 您的问题是您的验证摘要在您的表单中。移动这一行

<%: Html.ValidationSummary("Formuläret är inte korrekt ifyllt", new { @class = "errList" })%>

在此之前:

 <% using (Html.BeginForm("Register", "Ad", FormMethod.Post, new { enctype = "multipart/form-data" }))
           {%>

一切都应该工作得很好!!