2
\$\begingroup\$

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)

\$\endgroup\$
  • 1
    \$\begingroup\$ Is there code missing? Why are you making a database connection twice? \$\endgroup\$ – dreza Oct 26 '13 at 7:07
1
\$\begingroup\$

Can I access both RoleName and ID without fetching it in the backend?

Yes, I believe so if you want to write a bit of javascript. You would need to create a hidden field and on every select list change event set the value of that field.

Something like

$(body).on("change", "#selectbox", function() {
    $("#inputbox").val($(this).val());
});

I'm also unsure as to why you are creating the database connection twice but I would consider passing that into the GetRoles method like so:

public  IEnumerable<NewLogin> GetRolesForDDL(database db)
{
    return (from r in db.UserRole
            select new NewLogin
            {
                UserRole = r.Role,
                RoleID = r.RoleID
            });     
}    
public  NewLogin RoleDDL()
{
    using (database db = new database())
    {                               
        var roles = GetRolesForDDL(db).ToList();
        // populates ddl
        return new NewLogin
        {
            RoleList = new SelectList(roles, "RoleID", "UserRole");                
        }
    }
}

Or even if both of these methods are part of a class consider passing the database into the class constructor and using it as a private instance variable.

\$\endgroup\$
  • \$\begingroup\$ thats pointing that out about multiple db connections, it's unnecessary \$\endgroup\$ – CSharper Oct 28 '13 at 13:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.