MVC3-我应该将模型设计为与视图紧密耦合吗? [英] MVC3 - Should I design my Model to be tightly coupled to my View?

查看:77
本文介绍了MVC3-我应该将模型设计为与视图紧密耦合吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在使用MVC时,我发现我的视图在关联的模型定义方面是严格的.是否应该围绕视图的需求设计模型?我意识到我可以为我的视图专门创建一个容器.然后根据实体设计第二个模型.但是,似乎我总是需要这两者之间的隔stand.我的意思是,甚至还有一个@model来声明View应该耦合到什么.

In working with MVC I am finding my Views to be rigid in terms of their associated Model definition. Should I design my Models around the needs of my view? I realize I can create a container specifically for my view. Then design a second Model in terms of Entities. But, it appears that I always need this stand-between. I mean, there's even a @model to declare what the View is supposed to be coupled to.

例如,我有一个包含两个表的视图.这些表都在同一个实体上工作,因此将那个实体用作模型没有任何意义.相反,模型需要是一个包含2个上述实体的包装器.此外,实体确实需要转换为string[],以避免在View中进行数据批量处理.

For example, I have a View with two tables. The tables both work off the same Entity, so it doesn't make sense to use that Entity as the Model. Rather, the Model needs to be a wrapper that contains 2 of said entities. Moreover, the entities really need to be converted to string[] in order to avoid data massaging in the View.

我只是MVC小块太多了吗,或者这是MVC设计成如何工作的方式?与View-Model的紧密关系.

Am I just too much of an MVC nublet, or is this how MVC is designed to work? Tight relationship with View-Model.

推荐答案

使用

Use a ViewModel for your views. It's bad practice to associate your View with the Model that comes directly from EF.

public class ProductViewModel
{
    public ProductViewModel(List<Product> products, List<Category> categories)
    {
        this.Products = products;
        this.Categories = categories;
    }

    public List<Product> Products { get; private set; }
    public List<Category> Categories { get; private set; }

}

ViewModel最佳做法
ASP.NET MVC视图模型模式

这篇关于MVC3-我应该将模型设计为与视图紧密耦合吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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