Krishna Rana

Krishna Rana

  • NA
  • 362
  • 10.7k

Dropdown Required and Display attribute not working

Apr 12 2019 2:29 AM
Can we make dropdown required to select which is binding using ViewBag as below-
  1. // GET: Employee/Create in the action method  
  2. public ActionResult Create()  
  3. {  
  4. ViewBag.DepartmentId = new SelectList(db.Departments, "Id""Name");  
  5. return View();  
  6. }  
  7. // GET: Employee/Create in the Create view  
  8. @Html.LabelFor(model => model.DepartmentId, "DepartmentId", htmlAttributes: new { @class = "control-label col-md-2" })  
  9. @Html.DropDownList("DepartmentId"null"Select Department", htmlAttributes: new { @class = "form-control" })  
  10. @Html.ValidationMessageFor(model => model.DepartmentId, ""new { @class = "text-danger" })  
  11.   
  12. // setting metadata  
  13. [MetadataType(typeof(EmployeeMetaData))]  
  14. public partial class Employee  
  15. {  
  16. }  
  17. public class EmployeeMetaData  
  18. {  
  19. [Required]  
  20. public int EmployeeId { getset; }  
  21. [Required]  
  22. public string Name { getset; }  
  23. [Required]  
  24. public string Gender { getset; }  
  25. [Required]  
  26. public string City { getset; }  
  27. [Required]  
  28. [Display(Name = "Department")]  
  29. public Nullable<int> DepartmentId { getset; }  
i have set attribute Required and Display but both are not working,
 
pls help me.

Answers (5)