2011-01-25 18 views
0

嗨,問題與ValidationMessageFor

我已經把下面的代碼我的網頁上:

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

然後在每個屬性我的財產以後這樣的:

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

的問題是隻要我打開頁面,每個ValidationMessageFor都會顯示*,並且即使表單沒有,ValidationSummary也會顯示「表單不正確」尚未驗證?摘要列表將在表單驗證通過後首先顯示。

我需要ValidationMessageFor和ValidationSummary僅在表單驗證通過時顯示。

我錯過了什麼?

編輯1:屬性確實有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

請顯示您的控制器代碼? – Jimmy 2011-01-25 19:00:55

回答

1

我有轉載您的問題。 您的問題是您的驗證摘要是在您的表單內。此行之前移動此行

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

要:

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

,一切都應該工作得很好!