如何在MVC 5中搭建视图模型 [英] How To Scaffold a View Model in MVC 5

查看:694
本文介绍了如何在MVC 5中搭建视图模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试开发一个简单的应用程序.我通过实体框架引入了三个SQL表,并自动创建了模型.我希望能够在Visual Studio中自动创建创建/详细信息/编辑等"视图.当我从单个模型(例如单独的名称)建立支架时,我可以自动执行此操作,但是在将视图模型用作源时无法到达任何地方.

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.

这是我的模特

名称

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; }
}

地址

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; }
}

电子邮件

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; }
}

我可以完成创建控制器的步骤-使用Entity Framework,右键单击Controllers >> Add >> Controller >> MVC 5 Controller with views.

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.

如果单击添加,将出现以下错误.

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

我已经阅读了其他答案,如果您使用的是View Model,则需要清除Data上下文类(从第一张图像中),但是如果这样做,则Add按钮将被停用.我不能比这更进一步.这里有什么想法吗?

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?

推荐答案

我敢打赌,原始发布者这次可能仍未在寻找答案.但这可以帮助像我这样的求职者.

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

似乎在使用 Controller-> Add-> New Scaffolded Item->具有视图的MVC Controller时,使用实体框架似乎不适用于视图模型.

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.

如果在选择视图模型时在上述脚手架过程中未提供DataContext类,则MVC脚手架将不允许您继续进行.如您所指示,添加"按钮被禁用.

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.

首先使用脚手架创建控制器动作( Controllers-> Add-> New Scaffolded Item->具有读/写动作的MVC Controller )

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

然后通过右键单击各个控制器操作方法并利用脚手架来添加视图.(控制器的Action方法->右键单击->添加视图->模板-> [选择空(没有模型,但没有模型)]->模型类-> [在此处选择视图模型]-> 将数据上下文类留空->添加按钮将被启用.)

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.

但是,您仍然需要自己添加代码,才能在控制器操作方法中使用实体框架使用数据库.(或者,您可以选择引入Busines图层,存储库等. YMMV )但这有助于避免编写大量代码来创建视图.

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:我发现使用ASP.Net core 1.1时,这种方法对我来说很好用

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

这篇关于如何在MVC 5中搭建视图模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆