0
votes

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.

1
employees.Where(employee => employee.IsSuperviseur) would return the desired data.Kilazur
AJAX is completely unnecessary. Follow @Kilazur's advice. Create a select list from that filtered employee data, and you're done.Chris Pratt
Why do you use a ViewBag in your Employee Controller?Jeroen Heier
@Kilazur: I tried but I think I'm having trouble because IsSupervisor is nullable. Would you be kind enough to show me a precise example, please (where/how)? I'm not sure if I put my changes at the right places. As I said, I lack your experience. Thank you. I would really want to mark this question as solved.The girl with red hair
@Jeroen: My database is pretty complex (17 main tables + 6 "many-to-many" tables), so that's how it was scaffolded.The girl with red hair

1 Answers

0
votes

Added Line #8 From EmployesController:

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");            
        ViewBag.IdSuperviseur = new SelectList(db.Employe.Where(employe => employe.IsSuperviseur.HasValue && employe.IsSuperviseur.Value), "IdEmploye", "NomSuperviseur");
        return View();
    }

In order to have the supervisor full name, I added this in my Models/Employe.cs :

public string NomSuperviseur
    {
        get
        {
            return string.Format("{0} {1}", PrenomEmploye, NomEmploye);
        }
    }

P.S. The "NomSuperviseur" in my controller comes from these few lines of code, since originally, I could only put last name OR first name in my DropDownList.

And now Tadaaa! The magic is here! And no 'System.Web.Mvc.SelectListItem' appears in my list. It works perfectly, even in my database. View Code, lines 4-5:

        <div class="form-group">
        @Html.LabelFor(model => model.IdSuperviseur, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">                
            @Html.DropDownListFor(model => model.IdSuperviseur,
                (SelectList)ViewBag.IdSuperviseur, "", new { @class = "form-control" })
            @Html.ValidationMessageFor(model => model.IdSuperviseur, "", new { @class = "text-danger" })
        </div>
    </div>