【问题标题】:Problem with ValidationMessageForValidationMessageFor 的问题
【发布时间】:2011-01-27 17:51:26
【问题描述】:

嗨,

我在我的页面上放置了以下代码:

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

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

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

问题是,只要我打开页面,每个 ValidationMessageFor 都会显示 * 并且 ValidationSummary 会显示“表单不正确”,即使表单尚未经过验证?然而,当表单被验证时,摘要列表将首先显示。

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

我错过了什么?

Edit1:属性确实有 DataAnnotations。

编辑2:

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

<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; }
        }

【问题讨论】:

  • 请显示您的控制器代码?
  • 你的视图是什么样的?
  • 部分ViewClass和ViewPage贴出来了,真的需要一部分action code来吗?它的很多代码确实与验证几乎没有什么不同。
  • 如果您从 ModelViewAdRegister 中删除 PropertiesMustMatchAttribute 那么您还有问题吗?我之所以问,是因为我已经阅读了有关此属性的一些问题。
  • 谢谢,今晚我会尝试并返回结果。

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


【解决方案1】:

我已经复制了您的问题。 您的问题是您的验证摘要在您的表单中。移动这条线

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

到此行之前:

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

一切都应该正常工作!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-05
    • 2013-02-14
    • 2015-02-20
    相关资源
    最近更新 更多