Ok, I'm pretty new to MVC / C#. I really searched online to get an answer about that and I have been struggling for a long time already.
In my project, I have an Employee model:
- LastName NVARCHAR(50) NOT NULL,
- FirstName NVARCHAR(50) NOT NULL,
- ...
- IdSupervisor INTEGER, NULL,
- IsSupervisor BIT, NULL
When I create a new employee, I check a radio button if this employee is a supervisor. It goes well into my database as True/False.
What I want to do is dynamically create a DropDownList from that True/False property, that will show the first and last names, but will enter the IDSupervisor(that was created as an Employee first).
Example of Employee table data:
- Chaplin, Charlie, 1, Yes
- Keaton, Buster, 2, Yes
- Pickford, Mary, 1, Yes
- Fairbanks, Douglas, 2, No
- Laurel, Stan, 1, No
- Hardy, Oliver, 2, NO
Example of Supervisor DropDownList:
- Charlie Chaplin
- Buster Keaton
- Mary Pickford
I know how to create a static DropDownList, but it will be useless for this problem.
My code:
Employee Model:
public Employe()
{
this.AccesApplicatif1 = new HashSet<AccesApplicatif>();
this.TelephoneCellulaire1 = new HashSet<TelephoneCellulaire>();
this.CleBatiment1 = new HashSet<CleBatiment>();
this.EquipementInfo = new HashSet<EquipementInfo>();
this.GroupeSecurite1 = new HashSet<GroupeSecurite>();
this.VehiculeCompagnie1 = new HashSet<VehiculeCompagnie>();
}
public int IdEmploye { get; set; }
public string NomEmploye { get; set; }
public string PrenomEmploye { get; set; }
public int IdSuperviseur { get; set; }
public Nullable<bool> IsSuperviseur { get; set; }
Employee Controller:
// GET: Employes/Create
public ActionResult Create()
{
ViewBag.IdDepartement = new SelectList(db.Departement, "IdDepartement", "Description");
ViewBag.IdEmployeur = new SelectList(db.Employeur, "IdEmployeur", "NomEmployeur");
ViewBag.IdLocalisation = new SelectList(db.Localisation, "IdLocalisation", "Description");
ViewBag.IdTelephoneBureau = new SelectList(db.TelephoneBureau, "IdTelephoneBureau", "NumeroInventaire");
ViewBag.IdTitre = new SelectList(db.TitreEmploye, "IdTitre", "Description");
return View();
}
Create View:
<div class="form-group">
@Html.LabelFor(model => model.IdSuperviseur, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.IdSuperviseur, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.IdSuperviseur, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.IsSuperviseur, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
<div class="checkbox">
@Html.RadioButtonFor(model => model.IsSuperviseur, false, new { id = "rbtNonSuperviseur" })
@Html.Label("rbtNonSuperviseur", "Non")
@Html.RadioButtonFor(model => model.IsSuperviseur, true, new { id = "rbtOuiSuperviseur" })
@Html.Label("rbtOuiSuperviseur", "Oui")
@Html.ValidationMessageFor(model => model.IsSuperviseur, "", new { @class = "text-danger" })
</div>
</div>
</div>
ANY help will be greatly appreciated. Disclaimer : No celebrities were harmed during this post.
employees.Where(employee => employee.IsSuperviseur)
would return the desired data. – Kilazur