I'm currently populating my drop downlists like this...
public List<NewLogin> GetRolesForDDL()
{
using (database db = new database())
{
return (from r in db.UserRole
select new NewLogin
{
UserRole = r.Role,
RoleID = r.RoleID
}).ToList();
}
}
public NewLogin RoleDDL()
{
NewLogin nl = new NewLogin();
using (database db = new database())
{ // populates ddl
nl.RoleList = new SelectList(GetRolesForDDL(), "RoleID", "UserRole");
}
return nl;
}
The part I don't like in particular is where nl.RoleList = new SelectList(GetRolesForDDL()
Is there a better way I can do this, by keeping within the same style and bypassing that method and using a quick and easy lambda expression instead?
Also another quick question is...
@Html.DropDownListFor(u => u.RoleID, Model.RoleList)
When I pass this to the controller I get the RoleID value, but my RoleName(text of ddl)
get's passed as null. Can I access both RoleName and ID without fetching it in the backend?
I know this doesn't work, but I'm thinking it would have to be something like
DDLFor(u => u.RoleID, u.RoleName)