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.

Mohsin ArifPosted Apr 22, 2019, 12:38 AM
Jignesh KumarPosted Apr 22, 2019, 12:04 AM
Amit KumarPosted Apr 21, 2019, 11:40 PM
Laxmidhar SahooPosted Apr 20, 2019, 8:55 AM
Piyush PansuriyaPosted Apr 12, 2019, 5:15 AM