Liviu Sosu
Liviu Sosu

Reputation: 1557

How to have a selecteditem "focused" in a dropdown list (asp.net mvc 5) in a dynamically way?

I have table called Roles with the following columns: RoleID, RoleName. The rows of the table are :

RoleID RoleName

1 .NET Developer

2 Java Developer

3 Tester

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:

EmployeeID Name RoleID

1 John 1

2 Dan 2

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.

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)

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.

The Edit method from the controller which I believe is correct is the following:

    [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);
    }

The code from the view wich I believe I should modify it but I don't know how is the following:

 <div class="form-group">
            @Html.LabelFor(model => model.RoleID, "RoleID", htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.DropDownList("RoleID", null, "The Acctual Employee Role", htmlAttributes: new { @class = "form-control" })
                @Html.ValidationMessageFor(model => model.RoleID, "", new { @class = "text-danger" })
            </div>
        </div>

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).

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.

Any help will be appreciated!

Upvotes: 1

Views: 934

Answers (1)

user3559349
user3559349

Reputation:

Change your selectlist to (so it does not have the save name as the property name you are binding to)

ViewBag.RoleList = new SelectList(...

and your dropdown to

@Html.DropDownListFor(m => m.RoleID, (SelectList)ViewBag.RoleList, "The Actual Employee Role", htmlAttributes: new { @class = "form-control" }

The initial selected item will now be the value of employeeToUpdate.RoleID

Upvotes: 2

Related Questions