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 6166389
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T22:19:49+00:00 2026-05-23T22:19:49+00:00

Ok so here is my problem. I’m trying to populate an @Html.DropDownListFor() with my

  • 0

Ok so here is my problem. I’m trying to populate an @Html.DropDownListFor() with my Roles minus the Admin Role. This works just fine, but it shows all roles:

@Html.DropDownListFor(m => m.RoleName, new SelectList(Roles.GetAllRoles()))

However this shows all roles including the Admin roll.

So I have created another class UserHelper.cs with this Method that is basicly the same thing as Roles.GetAllRoles():

public string[] GetUserRoles()
    {
        string[] userroles = null;
        using (MainVeinDataDataContext conn = new MainVeinDataDataContext())
        {
            userroles = (from r in conn.Roles
                         where r.Rolename != "Admin"
                         select r.Rolename).ToArray();
        }
        return userroles;
    }

However, being very new to MVC, I have no idea how to expose this Method to the DropDownList in the view. So this doesn’t work no matter what I try:

@Html.DropDownListFor(m => m.RoleName, new SelectList(GetUserRoles()))

I’m not sure what I am missing and it is driving me crazy. Hopefully someone out there knows what I’m missing.

  • 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-23T22:19:51+00:00Added an answer on May 23, 2026 at 10:19 pm

    A view should not be responsible for pulling data from some datasources. It should only be responsible for manipulating the data that it was passed by the controller under the form of a vie model.

    What you are trying to do is an anti-MVC pattern. The code that you have put in this GetUserRoles method is the controller (or a data access layer) responsibility and the result of it it should be part of your view model. So for example you would have the following view model:

    public class MyViewModel
    {
        public string RoleName { get; set; }
        public IEnumerable<SelectListItem> UserRoles { get; set; }
    }
    

    and then you will have a controller action which will populate this view model:

    public ActionResult Foo()
    {
        // The GetUserRoles could also be part of a repository
        // that you would invoke here
        var userRoles = GetUserRoles();
    
        // Now construct the view model that you will pass to the view
        var model = new MyViewModel
        {
            UserRoles = userRoles.Select(x => new SelectListItem
            {
                Value = x,
                Text = x
            })
        };
        return View(model);
    }
    

    and now in your view:

    @model MyViewModel
    
    @Html.DropDownListFor(
        m => m.RoleName, 
        new SelectList(Model.UserRoles, "Value", "Text")
    )
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Maddening problem here. When my page loads: <body onload=getClientDateTime();> It runs this function: document.getElementById('ClientDateTime').value=hello
What is the problem here? (Besides having redundant code). $.getJSON works as expected. However
Just a small SVN problem here. I setup my own SVN server Setting up
I have an interesting problem here I've been trying to solve for the last
I'm trying to solve the problem here but I don't know why my code
PROBLEM: Here's the code: import matplotlib.pyplot as plt plt.bar([1,2],[5,4]) plt.title('this is a very long
I have problem here, I need all the stuffs inside this $_SESSION['cart'] thing and
I have a problem here and it seems that my brain has just left
Here is problem I'm trying to solve. It's for logistics company. I got all
Weird problem here, I'm trying to use a global function to update my settings

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.