How To Scaffold a View Model in MVC 5

asp.net-mvc asp.net-mvc-scaffolding entity-framework viewmodel

Question

I'm trying to develop a straightforward application. Through Entity Framework, I imported three SQL tables, and the models were generated automatically. I want Visual Studio to automatically scaffold out the Create, Details, Edit, and other views. When I scaffold from a single model, such as Name alone, I can do this easily, but when I use a View Model as a source, I get stuck.

I have my models here.

Name

public partial class Name
{
    public Name()
    {
        this.Addresses = new HashSet<Address>();
        this.Emails = new HashSet<Email>();
    }

    public int ID { get; set; }
    public string FIRST_NAME { get; set; }
    public string LAST_NAME { get; set; }

    public virtual ICollection<Address> Addresses { get; set; }
    public virtual ICollection<Email> Emails { get; set; }
}

Address

public partial class Address
{
    public int ADDRESS_ID { get; set; }
    public int NameID { get; set; }
    public string ADDRESS_1 { get; set; }
    public string CITY { get; set; }
    public string STATE { get; set; }
    public string ZIP { get; set; }

    public virtual Name Name { get; set; }
}

Email

public partial class Email
{
    public int EMAIL_ID { get; set; }
    public int NameID { get; set; }
    public string EMAIL { get; set; }

    public virtual Name Name { get; set; }
}

and a View Model of all three that I produced.

public class MainVM
{
    public Name Name { get; set; }
    public Address Address { get; set; }
    public Email Email { get; set; }
}

I may follow the instructions for constructing a controller by selecting MVC 5 Controller with views from the Add >> Controller menu in the Controllers submenu of the right-click menu using Entity Framework.

I then arrive at this screen.

enter image description here

The next error will appear if I click Add.

enter image description here

I've read in previous answers that if you are using a View Model, you need to take out the Data context class (from the first image), but if I do that, the Add button is disabled. I'm at a point where I have to stop. Any suggestions here?

1
8
4/23/2015 4:09:19 PM

Popular Answer

ZZZ_tmp
6
6/30/2017 4:21:17 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow