Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6696251
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T06:19:10+00:00 2026-05-26T06:19:10+00:00

So, I am implementing ASP Membership and Role management in my application. I also

  • 0

So, I am implementing ASP Membership and Role management in my application. I also have a second User table with all non-membership related information. I set the E-mail as the username in Membership and as the foreign key in my User table.

I am customizing the registration page to include a dropdown so a manager can be selected when the account is created. The list of managers is generated by finding all Membership users with the role “Manager” then creating a collection of Users where the foreign keys match the results.

List<string> managerNames = new List<string>(Roles.GetUsersInRole("Manager"));
var managers = from m in _db.Users where managerNames.Contains(m.Email) select m;
ViewBag.managers = managers;

Now I have to use that collection of users to populate a dropdown in my view that has the Name attribute set to “ManagerID” (to match my RegistrationModel), the value of each option set to the primary key of the User, and the displayed text in the dropdown showing the DisplayName of the User model.

I can go through the tedious task of looping through my “managers” collection and populating a separate SelectListItem, then passing the SelectListItem into a @Html.DropDown("ManagerID", newSelectListItem), but that seems excessive. Is there a more direct (or acceptable) way to do this?

EDIT

I added this to my controller

    var selectList = new List<SelectListItem>();
    foreach (var manager in managers)
        {
            selectList.Add(new SelectListItem(){
                Value = manager.UserID.ToString(),
                Text = manager.DisplayName,
                Selected = false
            });
        }
        ViewBag.managers = selectList;

and this to my view

@Html.DropDownList("ManagerID", (List<SelectListItem>)ViewBag.managers)

and it works. Is this still the best approach?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T06:19:11+00:00Added an answer on May 26, 2026 at 6:19 am

    Is this still the best approach?

    No. The best approach is to use view models and forget about the existence of ViewBag/ViewData. So start by designing a view model which will meet the requirements of your view (display a ddl of managers):

    public class MyViewModel
    {
        [Required]
        public int? SelectedManagerId { get; set; }
        public IEnumerable<SelectListItem> Managers { get; set; }
    }
    

    and then have your controller action populate this view model and pass it to the view:

    public ActionResult Foo()
    {        
        var managers = ... query your repository to get them
        var model = new MyViewModel
        {
            Managers = managers.Select(x => new SelectListItem
            {
                Value = x.UserID.ToString(),
                Text = x.DisplayName
            })
        };
        return View(model);
    }
    

    and finally in your strongly typed view:

    @model MyViewModel
    ...
    @Html.DropDownListFor(
        x => x.SelectedManagerId, 
        Model.Managers, 
        "-- Select a manager --"
    )
    

    So everytime you employ ViewBag/ViewData in an ASP.NET MVC applications an alarm should ring telling you that there is a better way.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an ASP.NET 3.5 application using Windows Authentication and implementing our own RoleProvider.
Hy, I am implementing a asp.net web application, and I have two webform files,
Does anyone know of an ASP.NET guide to implementing OpenID and what information can
I am implementing a comet using AsyncHttpHandlers in my current asp.net application. According to
I've written some custom model binders (implementing IModelBinder) in our ASP.NET MVC application. I'm
I am implementing an eCommerce application using ASP.Net. I would like to know if
I'm implementing StructureMap in a multi-tenant ASP.NET MVC application to inject instances of my
I'm implementing a logging feature on a asp.net mvc2 application, that uses SqlServer2008 as
I'm implementing the repository pattern in my ASP.NET MVC application. So i was doing
I am implementing a SaaS application using ASP.Net MVC 2 and SQL Server database.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.