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

  • Home
  • SEARCH
  • 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 4000156
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T07:44:48+00:00 2026-05-20T07:44:48+00:00

I have an entity, lets say Person : class Person { id Id {

  • 0

I have an entity, lets say Person:

class Person
{
    id Id { get; set; }
    string Name { get; set; }
    int RoleId { get; set; }
}

This RoleId property corresponds to a role selected from a list of available Roles. I pass this list of roles to the view using the ViewData object:

List<SelectListItem> roles = new List<SelectListItem>()
{
    new SelectListItem(){ Text = "User", Value = "0"},
    new SelectListItem(){ Text = "Admin", Value = "1"}
};
ViewData["roles"] = roles;

(Note I use strings as values here as it doesn’t let me use integers?)

My View (it’s an “add” view, to create a new Person) inherits from the Person entity. I am unsure how to setup the dropdown list in the view so that the value of the selected item in the list gets set as the RoleId property value in the Person object that is sent back to the Controller (for the HttpPost method).

  • 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-20T07:44:49+00:00Added an answer on May 20, 2026 at 7:44 am
    <%= Html.DropDownList(
        "RoleId", 
        new SelectList(ViewData["roles"] as IEnumerable<SelectListItem>, "Value", "Text")
    ) %>
    

    This being said it is not a code that I would recommend. Everytime I see someone using ViewData my temperature raises.

    I would use view models:

    public class PersonViewModel
    {
        public string Id { get; set; }
        public string Name { get; set; }
    
        public string RoleId { get; set; }
        public IEnumerable<SelectListItem> Roles { get; set; }
    }
    

    and then in your controller:

    public ActionResult Index()
    {
        var model = new PersonViewModel
        {
            Roles = new[]
            {
                new SelectListItem { Text = "User", Value = "0" },
                new SelectListItem { Text = "Admin", Value = "1" }
            }
        };
        return View(model);
    }
    

    and in your strongly typed view:

    <%= Html.DropDownListFor(
        x => x.RoleId, 
        new SelectList(Model.Roles, "Value", "Text")
    ) %>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say I have this entity (for Hibernate): @Entity public class Person { @Id
Lets say we have this JPA-annotated class, with a property of type List. This
Let's say we have an entity @Entity public class Person { @Id int id;
Lets say i have this entity public class Address : Entity { public Address()
Lets say thay I have an entity with name A.It has two columns named
Lets say I have two tables like this: person: id, first_name, last_name, phone_id phone:
Let's say I have Person class Person{ @Id Integer id; @OneToOne @JoinColumn(name = person_id)
Lets say i have the following controller: // // GET: /Courses/Edit/5 public ActionResult Edit(int
Lets say we have User entity class. User can be friends with other users.
Lets say I have a class: public class Entity { ... public IEnumerable<Cousin> Cousins

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.