NeatoBandito
NeatoBandito

Reputation: 110

Multiple models in the same view?

I'm relatively new to MVC and am attempting a code-first approach to make a one-page form for the user to fill out and submit. I have currently a few model classes in a Domain folder, but need to have a view to display all properties from each model at once for the user.

In addition to that, I'm trying to make the models work with eachother. For example, one of the model classes contains the following:

namespace HROnboarding.Domain
{
    public class SoftwareNeeded
    {
        //Primitive Properties
        public int Id { get; set; }
        public string SoftwareName { get; set; } //piece of software (GIMP, VS13, etc)
        public bool isNeeded { get; set; } //whether or not it is needed by the user
    }
}

This, however, will be un-needed if the user selects that they will not need a computer at all. This property is kept in the model shown below:

namespace HROnboarding.Domain
{
    public class Employee
    {
        public int ID { get; set; }

        [DisplayName("First Name")]
        public string FirstName { get; set; }
        [DisplayName("Last Name")]
        public string LastName { get; set; }
        public string Department { get; set; }
        public string Position { get; set; }
        public bool NeedsPhone { get; set; }
        public bool NeedsComputer { get; set; }
        public bool NeedsCredit { get; set; }
    }
}

Currently, I have generated the MVC "out-of-the-box" HomeController and associated views for Employee only, and so it shows the above properties available to change to the user, and to submit.

I also have a DbContext in another folder, for each model, so that data can be generated and have it connected to a local SQL database.

How would I be able to get the SoftwareNeeded class to display on the same page, so that it would be available at the same time?

My guess is that I would need to somehow create a general view file to sort of encapsulate the data from both classes, and display that to the user. My first instinct is to put all the model class data into a table so that it can all be filled out at once and submitted as a whole.

I'm obviously not very sure where to go from here or if I'm even approaching the problem(s) correctly.

Any help is greatly appreciated, thank you.

Upvotes: 2

Views: 166

Answers (1)

Tom Studee
Tom Studee

Reputation: 10452

One way this is handled is through the use of a ViewModel, which would be composed of the model types needed by the view.

For example:

public class OnBoardEmployeeViewModel
{
   public Employee Employee { get; set; }
   public SoftwareNeeded SoftwareNeeded { get; set; }
}

Another option would be to compose your ViewModel of only the fields displayed in the view, and then construct instances of your Model classes on submit.

Some further reading on viewmodels in MVC: http://sampathloku.blogspot.com/2012/10/how-to-use-viewmodel-with-aspnet-mvc.html

Upvotes: 7

Related Questions