title: How to have a selecteditem "focused" in a dropdown list (asp.net mvc 5) in a dynamically way?
body: <p>I have table called Roles with the following columns: RoleID, RoleName. The rows of the table are :</p>
  
  <h2>RoleID   RoleName</h2>
  
  <p>1        .NET Developer</p>
  
  <p>2        Java Developer</p>
  
  <p>3        Tester</p>
  
  <p>I have another table called Employees with the following columns: EmployeeID, Name, RoleID (a one-to many relationship with the Roles table).The rows of the table are:</p>
  
  <h2>EmployeeID  Name    RoleID</h2>
  
  <p>1           John     1</p>
  
  <p>2           Dan      2</p>
  
  <p>In my ASP.NET MVC 5 application I'm doing a CRUD opperation on employees. When I want to add a emploee I will have, besides the Name field, I have a dropdown list with the role that I want to assign. If I want to create a employee everything works well.</p>
  
  <p>My problem is that, when I want to edit a employee my dropdown list does not work as I am expecting. Lets assume that I want to change Dan's role to a Tester (RoleID should be changed from 2 to 3)</p>
  
  <p>I click edit on the Edit view and the selection of the dropdown list is .NET developer (the first entity), although I want to be a Java Developer (the second entity). I know why. I do not know how to fix it.</p>
  
  <p>The Edit method from the controller which I believe is correct is the following:</p>
  
  <pre><code>    [HttpGet]
      public ActionResult Edit(int id=0)
      {
          Employee  employeeToUpdate = db.Employees.Find(id);
          Role selectedRole = employeeToUpdate.Role;
  
          ViewBag.RoleID = new SelectList(db.Roles, "RoleID", "RoleName",selectedRole);
          return View(employeeToUpdate);
      }
  </code></pre>
  
  <p>The code from the view wich I believe I should modify it but I don't know how is the following:</p>
  
  <pre><code> &lt;div class="form-group"&gt;
              @Html.LabelFor(model =&gt; model.RoleID, "RoleID", htmlAttributes: new { @class = "control-label col-md-2" })
              &lt;div class="col-md-10"&gt;
                  @Html.DropDownList("RoleID", null, "The Acctual Employee Role", htmlAttributes: new { @class = "form-control" })
                  @Html.ValidationMessageFor(model =&gt; model.RoleID, "", new { @class = "text-danger" })
              &lt;/div&gt;
          &lt;/div&gt;
  </code></pre>
  
  <p>I can delete the parameter "The Acctual Employee Role" but I will get the list of all roles froom the database without being selected the coresponding role (Java Developer).</p>
  
  <p>I tried, to replace the "The Acctual Employee Role" with a @ViewBag.AcctualRole wich was initialized on the controller. Unfortunately that does not work because I may not use dinamycally expresions there.</p>
  
  <p>Any help will be appreciated! </p>