Can we make dropdown required to select which is binding using ViewBag as below-
- // GET: Employee/Create in the action method
- public ActionResult Create()
- {
- ViewBag.DepartmentId = new SelectList(db.Departments, "Id", "Name");
- return View();
- }
- // GET: Employee/Create in the Create 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" })
- // setting metadata
- [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.


Piyush PansuriyaRank #454 · 5.5K reputationPosted Apr 12, 2019, 1:15 AM EDT
Laxmidhar SahooRank #383 · 6.6K reputationPosted Apr 20, 2019, 4:55 AM EDT
Amit KumarRank #459 · 5.4K reputationPosted Apr 21, 2019, 7:40 PM EDT
Jignesh KumarRank #91 · 22.9K reputationPosted Apr 21, 2019, 8:04 PM EDT
Mohsin ArifRank #732 · 3.2K reputationPosted Apr 21, 2019, 8:38 PM EDT