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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T23:54:40+00:00 2026-06-02T23:54:40+00:00

I’m having trouble figuring out how to do this. The examples out there are

  • 0

I’m having trouble figuring out how to do this. The examples out there are not helping me understand what I need.

View Model

public class ViewModelAddUser 
{
    StudentSchedulingDBContext ssDB = new StudentSchedulingDBContext();

    public User User { get; private set; }
    public SelectList Departments { get; private set; }

    public ViewModelAddUser()
    {
        // Not Sure what to put here
    }


}

DBContext of Table (Just to show table structure in code)

public class Department
{
    public int DepartmentID { get; set; }
    public string DepartmentName { get; set; }
}

Controller

public ActionResult AddUser()
    {

        return View(new ViewModelAddUser());


    }

I’m not quite sure how to form the rest. I’ve been trying the nerddinner and other questions, but I’m not getting this to work.

Update

**View Model:**


public class ViewModelAddUser 
{
    public IEnumerable<SelectListItem> Departments { get; set; }
}

Controller:

public ActionResult AddUser()
    {
        ViewModelAddUser model = new ViewModelAddUser()
        {
            Departments = db.Departments.Select(department => new SelectListItem {
                Value = department.DepartmentID.ToString(),
                Text = department.DepartmentName
            })
        };
        return View(model);  
    }
  • 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-02T23:54:42+00:00Added an answer on June 2, 2026 at 11:54 pm

    A View model should only contain the elements that will be used within the view. That shouldn’t include your db context. Likewise, make sure the user class you mention in the view model, is a view model for creating a user, and not your user domain model.

    I think your view model should probably look something like :

    public class ViewModelAddUser 
    {
        public UserViewModel User { get; set; } //depending on what your doing a string containing UserId might suffice here
        public IEnumerable<SelectListItem> Departments { get; set; }
        public string DepartmentId { get; set; } // this will be the department set by drop down list
    }
    

    To create the dropdownlist:

    StudentSchedulingDBContext ssDB = new StudentSchedulingDBContext(); //I'm assuming this can be queried to get your departments.
    ViewModelAddUser model = new ViewModelAddUser()
    {
        Departments = ssDB.Departments.Select(department => new SelectListItem {
             Value = department.departmentID,
             Text = department.departmentName
        }),
        User = //set user here if necessary
    }
    
    return View(model);
    

    To call the drop down list:

    @Html.DropDownListFor(m => m.DepartmentId, Model.Departments);
    

    Also check out the link Austin provided, Darin’s answer on that question might help you.

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I need a function that will clean a strings' special characters. I do NOT
I'm having trouble keeping the paragraph square between the quote marks. In firefox the
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.
In my XML file chapters tag has more chapter tag.i need to display chapters

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.