简体   繁体   English

在ASP.net MVC的ViewModel中定义“模型”类型的属性是否好

[英]Is it good to define properties of “Model” type in ViewModel, ASP.net MVC

I've read couple of posts on using ViewModel in ASP.net MVC and realized it is different from "ViewModel" in MV-VM pattern. 我已经阅读了几篇关于在ASP.net MVC中使用ViewModel的文章,并意识到它与MV-VM模式中的“ ViewModel”不同。

ViewModel are used in order to avoid having Model being accessed directly from View but is it good approach to have properties of type (defined at Model layer) in ViewModel? 使用ViewModel是为了避免直接从View访问模型,但是在ViewModel中具有类型属性(在模型层定义)是一种好方法吗? Which means eventually we need to include Model namespace to ViewModel. 这意味着最终我们需要将模型名称空间包括到ViewModel中。

Eg 例如

Model 模型
1. YesNoTBDValue entity/POCO class 1. YesNoTBDValue实体/ POCO类

public partial class YesNoTBDValue
{
    public int Id { get; set; }
    public string Name { get; set; }
}

2 YesNoTBDValue class used in Project entity (defined in Model itself) 2 项目实体中使用的YesNoTBDValue类(在模型本身中定义)

public partial class Project
{
     public virtual YesNoTBDValue IsAvailable { get; set; }
}

View Model 查看模型
1. ProjectEditViewModel 1. ProjectEditViewModel

public class ProjectEditViewModel
{
HERE TO INCLUDE YesNoTBDValue CLASS, I NEED TO INCLUDE MODELS 
OR THERE IS BETTER WAY?
    public List<YesNoTBDValue> YesNoTBDValues { get; set; }
    public int IsAvailableSelectedItemId { get; set; }
}

Controller 调节器
Project Controller (in edit action creating new instance of view model) 项目控制器(在编辑操作中创建视图模型的新实例)

ProjectEditViewModel projectEditViewModel = new ProjectEditViewModel
{
    YesNoTBDValues = db.YesNoTBDValues.ToList()
};

View 视图
Showing DropDownList from YesNoTBDValues list and keeping selected item in IsAvailableSelectedItemId 显示YesNoTBDValues列表中的DropDownList并将所选项目保留在IsAvailableSelectedItemId中

@Html.DropDownList("IsAvailableSelectedItemId ", 
new SelectList(Model.YesNoTBDValues, "Id", "Name",
            Model.IsAvailableSelectedItemId ))

Please suggest me how it should be properly coded. 请建议我应该如何正确编码。

Repeating question: Should ViewModel include Model's namespace? 重复的问题:ViewModel是否应包含Model的名称空间? In my example YesNoTBDValue is defined in Model and to use it I am using Model's namespace 在我的示例中,在模型中定义了YesNoTBDValue ,要使用它,我正在使用模型的名称空间

/ ANOTHER APPROACH / / 另一种方法 /

Not satisfied with my existing approach, I downloaded the Microsoft Nuget Gallery source code from github and realize that they have never used MODELS inside VIEWMODEL which makes sense to me. 我对现有方法不满意,因此从github下载了Microsoft Nuget Gallery源代码,并意识到他们从未在VIEWMODEL内使用过MODELS,这对我来说很有意义。 I changed above code a little bit ( in order to remove reference of Model from ViewModel ) and found it working great. 我对上面的代码做了一些更改( 以便从ViewModel中删除对Model的引用 ),并发现它工作得很好。

Here are my changes: 这是我的更改:

Model No Changes, Same as it is 型号 不变,不变

View Model 查看模型
1. Create Replica of YesNoTBDValue class say YesNoTBDValueViewModel 1.创建YesNoTBDValue类的副本,说YesNoTBDValueViewModel

public class YesNoTBDValueViewModel
{
   public int Id { get; set; }
   public string Name { get; set; }
}

2 Use this ViewModel in ProjectEditViewModel and remove Model reference 2在ProjectEditViewModel中使用此ViewModel并删除模型引用

public class ProjectEditViewModel
{
     public List<YesNoTBDValueViewModel> YesNoTBDValues {get;set;}
     public int IsAvailableSelectedItem {get;set;}
}

Controller Change in way of populating these values. 控制器更改以填充这些值的方式。 (In Edit action) (在编辑操作中)

ProjectEditViewModel projectEditViewModel = new ProjectEditViewModel
{
    YesNoTBDValues = db.YesNoTBDValues.Select(
                x => new LMSPriorTool.ViewModels.YesNoTBDValueVM
 {
    Id = x.Id,
    Name = x.Name
 }).ToList()
}

And found after these changes, it is working fine too. 经过这些更改后发现,它也可以正常工作。 I like this second approach as in this Models and ViewModels are totally separated from each other . 我喜欢第二种方法,因为本模型和ViewModel完全相互分离 Keeping this question open for further discussion. 让这个问题开放,以供进一步讨论。

Please throw some light if I'm missing something here. 如果我在这里错过了一些东西,请给我一些启示。

I try to keep the ViewModel containing only simple types which are natural in the context of the view. 我试图保持ViewModel只包含在视图上下文中自然的简单类型。 That way I keep rendering logic in the view to a minimum and keep the view clean. 这样,我将视图中的渲染逻辑保持在最低限度并保持视图整洁。

Your ViewModel can be represented very simply with something like: 您的ViewModel可以非常简单地表示为:

public class ProjectEditViewModel
{
    public int YesNoTBDValueSelected { get; set; }
    public SelectList YesNoTBDValueOptions { get; set; }
}

or 要么

public class ProjectEditViewModel
{
    public int YesNoTBDValueSelected { get; set; }
    public IEnumerable<SelectListItem> YesNoTBDValueOptions { get; set; }
}

Now logic for generating SelectList goes in your Project <-> ProjectEditViewModel mapping and is kept out of View . 现在,用于生成SelectList逻辑进入您的Project <-> ProjectEditViewModel映射中,并且不包含在View

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM