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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T17:33:51+00:00 2026-06-17T17:33:51+00:00

Probably simple, but I seem to be missing something. Two Models: public class Hardware

  • 0

Probably simple, but I seem to be missing something.

Two Models:

public class Hardware
{
    [Required]
    public int Id { get; set; }

    public int SerialNum { get; set; }
    public int ProductNum { get; set; }
    public string Notes { get; set; }
    public DateTime PurchaseDate { get; set; }
    public DateTime WarrantyExpiration { get; set; }

    public virtual Manufacturer Manufacturer { get; set; }
}

public class Manufacturer
{
    public int Id { get; set; }

    [Required]
    public string Name { get; set; }

    public virtual ICollection<Hardware> Hardware { get; set; }
}

When I go to the Hardware Create view, I want to to be able to select from a dropdown of Manufacturers, and when it submits it should establish a relationship between the piece of hardware and the select Manufacturer.

At the moment, I’ve been using the following to build a selectList in the controller

SelectList selectList = new SelectList(db.Manufacturers, "Id", "Name");
ViewBag.selectList = selectList;

And then casting it in the view:

@Html.DropDownListFor(model => model.Manufacturer, ViewBag.selectList as SelectList)\

However, it seems like there should be a better way to do this – perhaps creating a viewModel that inherits from Hardware with a SelectList typed property?

  • 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-17T17:33:52+00:00Added an answer on June 17, 2026 at 5:33 pm

    As your application gets more and more complicated, you will see your MVC application turning into M-VM-V-C, VM is dedicated ViewModels that usually adds all the things that your UI layer need in order to generate the UI.

    I personally wouldn’t go with inheritance in this case, because your ViewModel is not a specialized version of your Model. It’s just what your UI need to create a View (this is really up to you).

    My ViewModel would look something like this:

    public class HardwareVm
    {
         public Hardware Hardware { get; set; }
         public IEnumerable<SelectListItem> Manufacturers { get; set; }
    } 
    

    in the View:

    @Html.DropDownListFor(model => model.Hardware.Manufacturer,  Manufacturers)
    

    controller:

    var manufacturers = db.Manufacturers.Select(m => new SelectListItem {Text = m.Name, Value = m.Id });
    var model = new HardwareVm { Manufacturers = manufacturers };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm probably missing something simple, but I'm tired of fighting with it. There seem
I know this is probably something simple but I can't seem to find anything
This is probably something really simple but for some reason I just can't seem
This is probably (hopefully) a pretty simple question, but I can't seem to get
I am probably missing something obvious with this problem, but i can't seem to
This is probably simple but I do not seem to get it to work.
This is probably a really simple question but I can't seem to get my
I'm probably missing something easy, but I seem to be blocked here... I have
This is probably a simple question but I can't seem to figure out how
This is probably a simple one but I can't seem to figure it out.

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.