Note that there are some explanatory texts on larger screens.

plurals
  1. PONot able to display error message in Dropdownlist In MVC
    text
    copied!<p>I am really stuck here for a long time and I don't know why its not working. I followed this <a href="https://stackoverflow.com/questions/4729440/validate-a-dropdownlist-in-asp-net-mvc">post</a> where it explained how to implement validation in MVC. But not working for me. </p> <p>My problem is I am able to display list in drop down but it is not showing any error message when I am in default option which was supposed to display error message.</p> <p><strong>My code is show below :</strong></p> <p><strong>Model class :</strong></p> <pre><code>public class ValidationModel { public IEnumerable&lt;SelectListItem&gt; options { get; set; } [Required(ErrorMessage = "Please select atleast one mode")] public int optionselected { get; set; } } </code></pre> <p><strong>Controller :</strong></p> <pre><code>public ActionResult validation() { var validation = new ValidationModel(); validation.options = new List&lt;SelectListItem&gt;() { new SelectListItem() {Text = "Add or Update Customer", Value = "1" }, new SelectListItem() { Text = "Update Customer Credit", Value = "2"} }; return View(validation); </code></pre> <p><strong>View :</strong></p> <pre><code>@using (Html.BeginForm("Validation", "home",FormMethod.Post)) { @Html.DropDownListFor( m =&gt; m.optionselected, Model.options, "Select List") @Html.ValidationMessageFor(m =&gt; m.optionselected) &lt;input type="submit" value="OK" /&gt; } </code></pre> <p>Here by default <strong>select list</strong> option will be displayed in my drop downlist. But here I want my error message to be displayed. But it is not displaying. When user select value 1 or 2 then this error message should not display. </p> <p>I tried using Modelstate also in my controller but not working :</p> <pre><code>[HttpPost] public ActionResult Validation() { var validation = new ValidationModel(); validation.options = new List&lt;SelectListItem&gt;() { new SelectListItem() {Text = "Add or Update", Value="1"}, new SelectListItem() {Text = "Update Customer", Value="2"} }; if (!ModelState.IsValid) { return View(model); } return RedirectToAction("bill"); } </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