I made a simple mvc 3 web site. It’s working fine but the database and classes are messy because I needed to make dropdownlists and I have no idea how to implement the drowdownlists using a different style.
Here’s how things look like:
public class Departments
{
public int ID {get; set;}
...
}
public class Users
{
public int ID {get; set;}
...
public Department Dept {get; set;}
public List<Department> DeptList {get; set;}
public string SelectedDept {get; set;}
}
then I used DropDownListFor helper. I’m new on this code first thing, sadly. So, now I got these 2 extra unnecessary columns along in the database. Any other way to do this cleaner/better?
Your class should look like this
Then in your MVC .NET Controller you perform a query to populate List DeptList.
DeptList can be stored in ViewBag.DeptList or as a property of the Model you return to the view.
string SelectedDept is not needed. When you use DropDownListFor helper it will automatically set Users.DeptId for you.
Also you may want to consider renaming “Users” to “User” =)