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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T20:03:31+00:00 2026-05-30T20:03:31+00:00

First, im sorry to my bad english :). My object have one variable of

  • 0

First, im sorry to my bad english :).

My object have one variable of type String and one selectListItem who have differents role.
I would like display the username and to each username their roles in the dropdownlist…

My class Modele User:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
using System.Web.Mvc;

namespace ASP.Models.User
{
    public class User
    {
        [Required(ErrorMessage = "User required")]
        [Display(Name = "Username")]
        public String Username { get; set; }

        [Display(Name = "Roles")]
        public IList<SelectListItem> roles = new List<SelectListItem>();
        public IList<SelectListItem> Roles
        {
            get { return roles; }
            set { roles = value; }
        }
    }
}

When the people connect on the page i will recover in the database the usernames and roles, after i put the roles in one list
The controler:

public ActionResult Modify()
        {
            Connection connection = new Connection();
            connection.creationConnexion();
            DbDataReader reader = connection.getBDAllPseudoRole();
            List<User> users=new List<User>();
            User user = null; ;
            if (reader.HasRows)
            {
                while (reader.Read())
                {
                    Boolean flag = false;
                    foreach (User userTemp in users)
                    {
                        if (userTemp.UserName.Equals(Convert.ToString(reader[0])))
                        {
                            user = userTemp;
                            flag = true;
                        }
                    }
                    if(flag==false)    
                        user = new user();

                    if (reader.GetValue(1) == DBNull.Value)//if first row equals null; we dont have the roles
                    {
                        user.UserName = Convert.ToString(reader[0]);

                    }
                    else
                    {
                        user.Username =  Convert.ToString(reader[0]);
                        SelectListItem item = new SelectListItem();
                        item.Text = Convert.ToString(reader[1]);
                        item.Value = Convert.ToString(reader[1]);
                        user.Roles.Add(item);
                    }
                    user.Add(user);
                }
    }
return View(users);
    }

My view:

@model ASP.Models.User
@{
    ViewBag.Title = "Modify";
}

<h2>Modify</h2>

@using (Html.BeginForm())
{
    @Html.ValidationSummary(true)
    <fieldset>
        <legend>User</legend>

        <div class="editor-label">
            @Html.LabelFor(user => user.UserName)
        </div>
        <div class="editor-field">
            @Html.EditorFor(user => user.UserName)
            @Html.ValidationMessageFor(user => user.UserName)
        </div>

        <div class="editor-label">
            @Html.DropDownList("roles", utilisateur => utilisateur.Roles)
        </div>

         <input type="submit" value="Connexion"/>

    </fieldset>
}

I have one error to display with the dropdownlist and i not find the solution.

Thanks,
Zoners

  • 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-30T20:03:32+00:00Added an answer on May 30, 2026 at 8:03 pm

    try this

    @Html.DropDownList("roles", Model.Roles)

    You could also add another string property to your viewmodel called Role

    public string Role{ get; set; } and use a strongly typed DropDownList as below

    @Html.DropDownListFor(model=>model.Role, Model.Roles)

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

Sidebar

Related Questions

First, sorry for my bad english. I would like when one people click on
First of all, sorry for my bad English... I have a problem with one
First, sorry for my bad english, I'm French. Here the problem : I have
First, sorry for bad english :-) On a little website i own, i would
(first, sorry for my bad english, i'm a beginner) I have a chart of
first, sorry for the bad english. I have a test that submit a login
First sorry for my bad English. I have this menubar in PHP. I want
First thanks for reading me and sorry for my bad english. I have the
First, sorry for my bad english. I have an EF entity that looks like:
First, sorry for my bad English. I'm making a Coupons site and have trouble

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.