1

I have a drop down list in MVC

Model:

[Required]  
[Display(Name = "State of Residency:")]  
public string StateCode { get; set; }

My View:

@Html.DropDownList("StateCode", "Select")
@Html.ValidationMessageFor(model => model.StateCode)

The drop down list works fine but how to make the validator validate the select. So it say please select a value in Select is there.

tereško
  • 58,060
  • 25
  • 98
  • 150
user3798891
  • 15
  • 1
  • 2
  • [here](http://stackoverflow.com/questions/4729440/validate-a-dropdownlist-in-asp-net-mvc) very nice example – Sergey Jul 31 '14 at 21:42
  • Thank you, but I'm knew to mvc I'm using two tables with FK for StateCode, The problem is that the validator assume that Select is an imput. – user3798891 Aug 01 '14 at 22:52

1 Answers1

1

Example for ASP.NET MVC 5 web application:

In your case I would make like so:

1) Enable validation in web.config

<appSettings>    
   <add key="ClientValidationEnabled" value="true" />
   <add key="UnobtrusiveJavaScriptEnabled" value="true" />
</appSettings>

2) add proper jQuery files

 <script src="~/Scripts/jquery-1.11.1.min.js"></script>
 <script src="~/Scripts/jquery.validate.min.js"></script> <!--v1.13.0-->
 <script src="~/Scripts/jquery.validate.unobtrusive.min.js"></script> <!-- For ASP.NET MVC 5.0 -->

3) StateCode.cs

 public class StateCode
 {
     public int Id { get; set; }
     public int Code { get; set; }
 }

4) MyModel.cs

 public class MyModel
 {
     [Required(ErrorMessage = "Please select")]
     public int IdSelected { get; set; }

     [Display(Name = "State of Residency:")]
     public IEnumerable<StateCode> StateCodes { get; set; }
 }

5) Action

 [HttpGet]
 public virtual ActionResult Index()
 {
      var model = new MyModel
      {
          StateCodes = new List<StateCode>
          {
              new StateCode{Id=1, Code=22333}, 
              new StateCode{Id=2, Code=44433},  
              new StateCode{Id=3, Code=55533},  
          }
      };
      return View(model);
  }

6) View

  @model MyModel

  @using (Html.BeginForm())
  {
     @Html.DropDownListFor(model => model.IdSelected, new SelectList(Model.StateCodes, "Id", "Code"), "Please Select")
     @Html.ValidationMessageFor(model => model.IdSelected)

     <input type="submit" value="OK" />
  }

btw. here very good list of jQuery files for ASP.NET MVC unobtrusive validation.

Community
  • 1
  • 1
Sergey
  • 471
  • 3
  • 10
  • After I tried this: @Html.DropDownListFor(x => x.StateCode, new SelectList(Model.State, "StateCode", "StateName"), "Pleasesssssss") I'm getting The best overloaded method match for "System.Web.Mvc.Selectlist.Selectlist(System.Colections.IEnumerable, string, string)' has some invalid arguments – user3798891 Aug 01 '14 at 22:54
  • `x.StateCode` should be `int` and `Model.State` should be `IEnumerable` - do you have the same? btw. it would be better if you will rename `Model.State` to `Model.States` as it's list – Sergey Aug 02 '14 at 11:10
  • Thank you for the suggestion I agree it should be states. The solution above worked for me – user3798891 Sep 03 '14 at 15:54