Click here to Skip to main content
15,905,322 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi Friends,
I need to validate the dropdownlist in model at mvc 4 . I have posted my code here .

My Model
        View

      @Html.LabelFor(M => Model.Patient.DoctorID)
      @Html.DropDownList("doctorType")


             Controller

      public List<SelectListItem> SelectDoctor()
{
    Gema_Doctor[] doctorList;
    doctorList = gema_Doctor.GetDoctor().ToArray();
    List<gema_doctor> listDoctor = new List<gema_doctor>(doctorList);
    List<SelectListItem> dropItems = new List<SelectListItem>();
    dropItems.Add(new SelectListItem { Text = "--Select--", Value = "", Selected = true });
    foreach (Gema_Doctor doctorValues in listDoctor)
    {
        dropItems.Add(new SelectListItem { Text = doctorValues.Dr_Name, Value = doctorValues.Dr_Id.ToString() });
    }
    return dropItems;
}  .

              Model
     [Required(ErrorMessage="Please Select Doctor")]
     [Display(Name = "DoctorID")]
     public int DoctorID { get; set; }
Posted
Updated 10-Nov-13 20:31pm
v4

1 solution

you can use javascript for this

onclick function of button write a javascript to check what is the value in dropdown list according to that show message

if not then just show me what isthe drror message you are getting
 
Share this answer
 
Comments
MurugappanCTS 8-Nov-13 6:53am    
I do not get errors. but Above code is not validating clientside validation for dropdownlist

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900