Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How To Scaffold a View Model in MVC 5

I'm trying to work on a simple application. I have three SQL tables brought in through Entity Framework and had the models created automatically. I want to be able to scaffold out the Create/Details/Edit etc. views automatically in Visual Studio. I can do this automatically when I scaffold from a single model (like Name alone), but can't get anywhere when using a View Model as a source.

Here are my models

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 I created of all three

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

I can go through the steps of creating a controller - Right click Controllers >> Add >> Controller >> MVC 5 Controller with views, using Entity Framework.

Next I get to this screen.

enter image description here

If I click Add, I will get the following error.

enter image description here

I've read in other answers that you need to clear out the Data context class (from the first image) if you are using a View Model, but if I do that, the Add button becomes deactivated. I can't go further than that. Any ideas here?

like image 200
madvora Avatar asked Apr 23 '15 16:04

madvora


People also ask

How do you scaffold a view?

To add a scaffold, right-click on Controllers folder in the Solution Explorer and select Add → New Scaffolded Item. It will display the Add Scaffold dialog. Select MVC 5 Controller with views, using Entity Framework in the middle pane and click 'Add' button, which will display the Add Controller dialog.

What is scaffolding in MVC with example?

Scaffolding is used to define the code-generation framework used in web applications. It uses T4 templates to generate basic controllers and views for the models. It generates instances for the mapped domain model and code for all CRUD operations.

How do you add scaffolding?

To add a scaffold, right-click either the project or a folder within the project, and select Add – New Scaffolded Item, as shown in the following image. From the Add Scaffold window, select the type of scaffold to add.


1 Answers

I bet the original poster might not still be looking for an answer by this time. but it can help seekers like me..

Found this article be of some help. Link

It seems while taking the route Controller -> Add -> New Scaffolded Item -> MVC Controller with views, using Entity Framework does not work well with view models.

If you did not provide a DataContext class in the above mentioned scaffolding process while selecting your viewmodel, MVC scaffolding will not allow you to proceed further. As you indicated the "Add" button is disabled.

The workaround is to take a two step approach.

First create controller actions using scaffolding (Controllers -> Add -> New Scaffolded Item -> MVC Controller with read/write actions)

And then add views by right clicking on individual controller action methods and then taking advantage of scaffolding. (Controller's Action method -> Right click -> Add View -> Template -> [choose anything but Empty(without model)] -> Model class -> [choose your view model here] -> Leave Data context class empty -> Add button will now be enabled).

The linked article covers the steps in detail please take a look.

However, you will still need to add code yourself to work with the database using Entity framework in your controller action methods. (Or you can choose to introduce Busines layers, repositories etc.. YMMV) But this helps avoid writing lots of code to create your views.

PS: I found this approach work just fine for me while using ASP.Net core 1.1

like image 135
blogs4t Avatar answered Sep 19 '22 19:09

blogs4t