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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T10:52:16+00:00 2026-06-05T10:52:16+00:00

Problem I am currently adding automapping to my MVC project and I am stuck.

  • 0

Problem

I am currently adding automapping to my MVC project and I am stuck. Right now I have a User model used to represent data in the database. I have to map that model to a EditUserModel which will be used when the Edit method is called. The EditUserModel has IEnumerable<SelectListItem> (for a dropdown menu) that I can’t seem to figure out how to map.

Attempted Solution

As of right now I haven’t tried to implement anything. I am unsure where the best place for the IEnumerable<SelectListItem> or where to populate it. Right now it is being populated in the Controller.

User.cs

public class User
{
    [Key]
    public int UserID { get; set; }

    public string Username { get; set; }

    public string Password { get; set; }

    public int RoleID { get; set; }

    [ForeignKey("RoleID")]
    public virtual Role Role { get; set; }
}

EditUserModel.cs

public class EditUserViewModel
{
    [HiddenInput(DisplayValue = false)]
    public int UserID { get; set; }

    [Required]
    public String Username { get; set; }

    [Required]
    [DataType(DataType.Password)]
    public string Password { get; set; }

    [DisplayName("Role")]
    [Required]
    public int RoleID { get; set; }

    //The trouble field
    public IEnumerable<SelectListItem> Roles { get; set; }
}

Controller.cs

EditUserViewModel model = new EditUserViewModel();
//Population of the dropdown menu
model.Roles = context.Roles
    .ToList()
    .Select(x => new SelectListItem
    {
        Text = x.Description,
        Value = x.RoleID.ToString()
    });
//Mapping that the automaper will take care of
model.UserID = user.UserID;
model.Username = user.Username;
model.RoleID = user.RoleID;
  • 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-06-05T10:52:18+00:00Added an answer on June 5, 2026 at 10:52 am

    For the record, here is what I was talking about in the comments to Jakub’s answer:

    public static class EnumerableExtensions
    {
        public static IEnumerable<SelectListItem> ToSelectList<T, TTextProperty, TValueProperty>(this IEnumerable<T> instance, Func<T, TTextProperty> text, Func<T, TValueProperty> value, Func<T, bool> selectedItem = null)
        {
            return instance.Select(t => new SelectListItem
            {
                Text = Convert.ToString(text(t)),
                Value = Convert.ToString(value(t)),
                Selected = selectedItem != null ? selectedItem(t) : false
            });
        }
    }
    

    Needless to say, this is dramatically simpler and accomplishes the same thing (and is actually more robust in the event that the property paths are non-simple, since Jakub’s solution will not work with nested properties).

    (This is not really an answer, I’m posting it as a community wiki just to help elaborate a point)

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

Sidebar

Related Questions

I currently have the following problem with Apache Tapestry 5.3.1: The user should be
I currently have a NetBeans php project setup to sync from my user directory
Currently, in my request model I have: belongs_to :requestor, :class_name => 'User' So the
My problem currently is that I've got a project on bitbucket, and I've been
I have a problem currently when i run the command mvn install, the artifact
I'm currently building a semi-small ruby app for a project. The problem I'm currently
I currently have a problem with my jgroups configuration, causing thousands of messages getting
I currently have a problem attepting to update a record within my database. I
I currently have a problem within PHP where I want to sort these posts
I have currently the following problem: The footers width gets smaller as soon as

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.