是好界定的&QUOT性能;型号:QUOT;键入视图模型,ASP.net MVC [英] Is it good to define properties of "Model" type in ViewModel, ASP.net MVC

查看:261
本文介绍了是好界定的&QUOT性能;型号:QUOT;键入视图模型,ASP.net MVC的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我读过一些文章对ASP.net MVC使用视图模型,实现它是从视图模型的不同之处M-V-VM模式。

I've read couple of posts on using ViewModel in ASP.net MVC and realized it is different from "ViewModel" in M-V-VM pattern.

视图模型的使用,以避免被模型从视图直接访问,但它的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? Which means eventually we need to include Model namespace to ViewModel.

例如。

型号结果
1. YesNoTBDValue实体/ POCO类

Model
1. YesNoTBDValue entity/POCO class

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

2 YesNoTBDValue中使用类的项目实体(在模式本身的定义)

2 YesNoTBDValue class used in Project entity (defined in Model itself)

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

视图模型结果
1. ProjectEditViewModel

View Model
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()
};

查看结果
从显示列表YesNoTBDValues​​ DropDownList的,并保持在IsAvailableSelectedItemId选择的项目

View
Showing DropDownList from YesNoTBDValues list and keeping selected item in IsAvailableSelectedItemId

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

请建议我应该如何妥善codeD。

Please suggest me how it should be properly coded.

重复问题:如果视图模型包括产品型号的命名空间?在我的例子 YesNoTBDValue 的模式,并使用它我使用的定义的型号的命名空间

Repeating question: Should ViewModel include Model's namespace? In my example YesNoTBDValue is defined in Model and to use it I am using Model's namespace

/ 另一种方法 /

不满意我的现行办法,我下载从GitHub微软的NuGet库源$ C ​​$ c和认识到内部视图模型他们从来没有使用的模型这对我来说很有意义。我上面$ C $改变℃的点点(的为了从视图模型的删除模型参考),并发现它伟大的工作。

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. I changed above code a little bit (in order to remove reference of Model from ViewModel) and found it working great.

下面是我的变化:

型号 不可更改,同是

视图模型结果
1.创建YesNoTBDValue类的副本说的 YesNoTBDValueViewModel

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

2使用该视图模型中的 ProjectEditViewModel 并删除模型参考

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()
}

,发现这些变化后,实在是太正常工作。我喜欢第二种方法在本模型和的ViewModels是完全相互分离。保持这个问题公开进行进一步讨论。

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. 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. That way I keep rendering logic in the view to a minimum and keep the view clean.

您的ViewModel可以重新presented很简单的东西,如:

Your ViewModel can be represented very simply with something like:

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

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

现在用于生成逻辑的SelectList 进入你的项目&LT; - > ProjectEditViewModel 映射和保持出查看

Now logic for generating SelectList goes in your Project <-> ProjectEditViewModel mapping and is kept out of View.

这篇关于是好界定的&QUOT性能;型号:QUOT;键入视图模型,ASP.net MVC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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