Can we make dropdown required to select which is binding using ViewBag as below-
-
- public ActionResult Create()
- {
- ViewBag.DepartmentId = new SelectList(db.Departments, "Id", "Name");
- return View();
- }
-
- @Html.LabelFor(model => model.DepartmentId, "DepartmentId", htmlAttributes: new { @class = "control-label col-md-2" })
- @Html.DropDownList("DepartmentId", null, "Select Department", htmlAttributes: new { @class = "form-control" })
- @Html.ValidationMessageFor(model => model.DepartmentId, "", new { @class = "text-danger" })
-
-
- [MetadataType(typeof(EmployeeMetaData))]
- public partial class Employee
- {
- }
- public class EmployeeMetaData
- {
- [Required]
- public int EmployeeId { get; set; }
- [Required]
- public string Name { get; set; }
- [Required]
- public string Gender { get; set; }
- [Required]
- public string City { get; set; }
- [Required]
- [Display(Name = "Department")]
- public Nullable<int> DepartmentId { get; set; }
i have set attribute Required and Display but both are not working,
pls help me.