Note that there are some explanatory texts on larger screens.

plurals
  1. POProblem with ValidationMessageFor
    primarykey
    data
    text
    <p><strong>Hi,</strong></p> <p>I have placed the following code on my page : </p> <pre><code>&lt;%: Html.ValidationSummary("Form not correct", new { @class = "errList" })%&gt; </code></pre> <p>Then on each property I have somthing like : </p> <pre><code>&lt;%: Html.ValidationMessageFor(model =&gt; model.ModelViewAd.Title, "*", new { @class = "val" })%&gt; </code></pre> <p>The problem is that as soon as I open the page each ValidationMessageFor will show * and the ValidationSummary will show "Form not correct" even when the form has not yet been validated? The Summary list will however be shown first when the form has been validated.</p> <p>I need both the ValidationMessageFor and the ValidationSummary to be shown only when the form has ben validated.</p> <p>What am I missing?</p> <p>Edit1: The Properties do have DataAnnotations.</p> <p>Edit2:</p> <p>This is how the first part of the view looks like</p> <pre><code>&lt;div id="adRegistrationForm"&gt; &lt;% Html.EnableClientValidation(); %&gt; &lt;h1&gt;Registrera din annons&lt;/h1&gt; &lt;%: Html.ValidationMessageFor(model =&gt; model.ModelViewAd, "*", new { @class = "val" })%&gt; &lt;br /&gt; &lt;% using (Html.BeginForm("Register", "Ad", FormMethod.Post, new { enctype = "multipart/form-data" })) {%&gt; &lt;%: Html.ValidationSummary("Formuläret är inte korrekt ifyllt", new { @class = "errList" })%&gt; &lt;div class="controlTitle"&gt; &lt;%: Html.LabelFor(model =&gt; model.ModelViewAd.Title)%&gt; &lt;%: Html.ValidationMessageFor(model =&gt; model.ModelViewAd.Title, "*", new { @class = "val" })%&gt; &lt;/div&gt; &lt;div&gt; &lt;%: Html.TextBoxFor(model =&gt; model.ModelViewAd.Title, new { @class = "tb1" })%&gt; &lt;/div&gt;&lt;br /&gt; &lt;div class="controlTitle"&gt; &lt;%: Html.LabelFor(model =&gt; model.ModelViewAd.TypeOfAd)%&gt; &lt;/div&gt; &lt;div&gt; &lt;%: MvcHtmlString.Create(Html.RadioButtonListFor(model =&gt; model.ModelViewAd.TypeOfAd)) %&gt; &lt;/div&gt;&lt;br /&gt; &lt;div id="divEndDate"&gt; &lt;div class="controlTitle"&gt; &lt;%: Html.LabelFor(model =&gt; model.ModelViewAd.EndDate)%&gt; &lt;%: Html.ValidationMessageFor(model =&gt; model.ModelViewAd.EndDate, "*", new { @class = "val" })%&gt; &lt;/div&gt; &lt;div&gt; &lt;%: Html.TextBoxFor(model =&gt; model.ModelViewAd.EndDate)%&gt; &lt;/div&gt; &lt;/div&gt; </code></pre> <p>And this is how the first part of the viewClass looks like :</p> <pre><code>[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; } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload