Note that there are some explanatory texts on larger screens.

plurals
  1. PODropDownListFor Unobtrusive validation shows on empty form
    text
    copied!<p>I get validation message always when I open this page (even first time), even if I choose value in one of dropdown lists, message doesn't go away. If I choose values in both I can submit form but messages still doesn't go away.</p> <p><code>Snippet</code> is Linq to sql class and <code>LanguageID</code> and <code>SnippetTypeID</code> are ints, I assume this happens because I pass empty model to View so <code>LanguageID</code> and <code>SnippetTypeID</code> are null and AFAIK Linq to Sql classes have required on non-nullable ints.</p> <p>How can I fix this so validation messages doesn't appear before user tries to submit form, and if one of dropdown lists get selected to remove validation message.</p> <p><strong>View</strong> </p> <pre><code>@model Data.Snippet @using (Html.BeginForm("Save", "Submit", FormMethod.Post)) { &lt;h1 class="subtitle"&gt;Submit new snippet&lt;/h1&gt; &lt;h4&gt;Title&lt;/h4&gt; @Html.TextBoxFor(snippet =&gt; snippet.Title, new { @class = "form-field" }) &lt;h4&gt;Language&lt;/h4&gt; @Html.DropDownListFor(snippet =&gt; snippet.LanguageID, new SelectList(@ViewBag.Input.Languages, "ID", "Name", @Model.LanguageID), "Choose Language", new { @class = "form-select" }) &lt;p&gt;@Html.ValidationMessageFor(snippet =&gt; snippet.LanguageID , "You must choose language", new { @class= "validation-message"})&lt;/p&gt; &lt;h4&gt;Snipet type&lt;/h4&gt; @Html.DropDownListFor(snippet =&gt; snippet.SnippetTypeID, new SelectList(@ViewBag.Input.SnippetTypes, "ID", "Name", @Model.SnippetType), "Choose snippet type", new { @class = "form-select" }) &lt;p&gt;@Html.ValidationMessageFor(snippet =&gt; snippet.SnippetTypeID,"You must choose snippet type", new { @class= "validation-message"})&lt;/p&gt; &lt;h4&gt;Text&lt;/h4&gt; @Html.TextAreaFor(snippet =&gt; snippet.Text, new { cols = "20", rows = "10", @class = "form-field" }) &lt;input type="submit" value="Submit Snippet" /&gt; } </code></pre> <p><strong>Controller</strong></p> <pre><code> //Controllers are not finished Save() should have //code to actually insert to db after I fix validation // GET: /Submit/ // public ActionResult Index() { Snippet model = new Snippet(); SubmitModel input = new SubmitModel(); ViewBag.Input = input; return View(model); } public ActionResult Save(Snippet snippet) { return View(); } </code></pre> <p><strong>Model</strong></p> <p>Model is Linq to Sql class.</p> <pre><code>Snippet ID (int, identifier) Title (string) SnippetType (int, FK on table SnippetTypes) LanguageID (int, FK on table Languages) Text (string) </code></pre>
 

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