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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T15:23:34+00:00 2026-05-20T15:23:34+00:00

After reading 100’s of articles on here about how to create a DropDown List

  • 0

After reading 100’s of articles on here about how to create a DropDown List in MVC 3 with Razor Views, I could not find one that fit my case.

Situation:
I am ultimately trying to create a View to Add an Employee to a Database.

Here is an image of the .EDMX Model that I am using (the tables that will be used by the create().):

enter image description here

Objectives:

  1. Create an Employee (I have the Create.cshtml (strongly typed) made with a Partial View for the StaffNotify Checkboxes) {I am using a separate @model in the Notify Partial View from the Create View not sure if that is safe??? @model ShadowVenue.Models.Employee & @model ShadowVenue.Models.StaffNotify)

  2. Create a Dropdown box for StaffTypeId (that will insert the [StaffTypeId] value from the Table “StaffType” (which has a 1 to many relationship), but will show the [Type] string value in the dropdown)

  3. Create a Dropdown box for GenderId (that will insert the [GenderId] value from the Table “Genders” (which has a 1 to many relationship), but will show the [Gender] string value in the dropdown)

  4. Insert the Record into the database (I have the Staff Notifications in a separate table with a 1 to 1 relationship on the StaffId Primary Key)

I seem to be having the trouble with the Controller code for this.

I am not sure if I should create Stored Procedure within the EDMX model, or come up with some query or method syntax, not sure which is the best way.

This my First Large MVC3 App using Entity Framework Model.

(if you need to know any of the Navigation Property Names in order to help with the solution just let me know, I will provide them to you)

  • 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-20T15:23:35+00:00Added an answer on May 20, 2026 at 3:23 pm

    Don’t pass db models directly to your views. You’re lucky enough to be using MVC, so encapsulate using view models.

    Create a view model class like this:

    public class EmployeeAddViewModel
    {
        public Employee employee { get; set; }
        public Dictionary<int, string> staffTypes { get; set; }
        // really? a 1-to-many for genders
        public Dictionary<int, string> genderTypes { get; set; }
    
        public EmployeeAddViewModel() { }
        public EmployeeAddViewModel(int id)
        {
            employee = someEntityContext.Employees
                .Where(e => e.ID == id).SingleOrDefault();
    
            // instantiate your dictionaries
    
            foreach(var staffType in someEntityContext.StaffTypes)
            {
                staffTypes.Add(staffType.ID, staffType.Type);
            }
    
            // repeat similar loop for gender types
        }
    }
    

    Controller:

    [HttpGet]
    public ActionResult Add()
    {
        return View(new EmployeeAddViewModel());
    }
    
    [HttpPost]
    public ActionResult Add(EmployeeAddViewModel vm)
    {
        if(ModelState.IsValid)
        {
            Employee.Add(vm.Employee);
            return View("Index"); // or wherever you go after successful add
        }
    
        return View(vm);
    }
    

    Then, finally in your view (which you can use Visual Studio to scaffold it first), change the inherited type to ShadowVenue.Models.EmployeeAddViewModel. Also, where the drop down lists go, use:

    @Html.DropDownListFor(model => model.employee.staffTypeID,
        new SelectList(model.staffTypes, "ID", "Type"))
    

    and similarly for the gender dropdown

    @Html.DropDownListFor(model => model.employee.genderID,
        new SelectList(model.genderTypes, "ID", "Gender"))
    

    Update per comments

    For gender, you could also do this if you can be without the genderTypes in the above suggested view model (though, on second thought, maybe I’d generate this server side in the view model as IEnumerable). So, in place of new SelectList... below, you would use your IEnumerable.

    @Html.DropDownListFor(model => model.employee.genderID,
        new SelectList(new SelectList()
        {
            new { ID = 1, Gender = "Male" },
            new { ID = 2, Gender = "Female" }
        }, "ID", "Gender"))
    

    Finally, another option is a Lookup table. Basically, you keep key-value pairs associated with a Lookup type. One example of a type may be gender, while another may be State, etc. I like to structure mine like this:

    ID | LookupType | LookupKey | LookupValue | LookupDescription | Active
    1  | Gender     | 1         | Male        | male gender       | 1
    2  | State      | 50        | Hawaii      | 50th state        | 1
    3  | Gender     | 2         | Female      | female gender     | 1
    4  | State      | 49        | Alaska      | 49th state        | 1
    5  | OrderType  | 1         | Web         | online order      | 1
    

    I like to use these tables when a set of data doesn’t change very often, but still needs to be enumerated from time to time.

    Hope this helps!

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

Sidebar

Related Questions

After reading a bit more about how Gnutella and other P2P networks function, I
After reading Practical Common Lisp I finally understood what the big deal about macros
After reading Evan's and Nilsson's books I am still not sure how to manage
After reading the Head First Design Patterns book and using a number of other
After reading this question , I was reminded of when I was taught Java
After reading this description of late static binding (LSB) I see pretty clearly what
After reading What’s your/a good limit for cyclomatic complexity? , I realize many of
After reading the answers to the question Calculate Code Metrics I installed the tool
After reading this answer: best way to pick a random subset from a collection?
After reading the Test-and-Set Wikipedia entry , I am still left with the question

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.