ASP.NET Core MVC中的Model和ViewModel有什么区别? [英] What is difference between Model and ViewModel in asp.net core mvc?

查看:49
本文介绍了ASP.NET Core MVC中的Model和ViewModel有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用于帐户模型的Account类.

I have a Account class for account models.

public class Account
{
    [Key]
    public Int64 UID { get; set; }

    [Required]
    public string ID { get; set; }

    [Required]
    public string PassWord { get; set; }
    [Required]
    public string UserName { get; set; }

}

我的项目不是代码优先项目,这是数据库帐户"的模型类.

My project is not a code first project and this is a model class for database 'Account'.

但是我在登录视图中仅使用两个属性,字符串ID和字符串PassWord.因此,当我检查模型的有效性时,由于无法使用ModelState.Isvalid(),因此无法在登录控制器中使用它只有两个属性...

But I use only two properties in login view, string ID and string PassWord. So I can not use ModelState.Isvalid() in the login controller when I check the validation of model because I use just two properties...

于是我进行了搜索,现在发现了"ViewModel",这是用于视图的模型类.

So I searched about that then, now I found about 'ViewModel' which is the model class for view.

然后我创建了一个新类'AccountViewModel',然后将其与视图而不是'Account'模型进行映射.

Then I created a new class 'AccountViewModel' and then I mapped this with view instead of 'Account' model.

我的方法正确吗?我知道ViewModel是仅用于View的模型类.并且Model类适用于所有人.(如全局含义...?对于数据库,视图等)

Did my way was right? I understood the ViewModel is a model class just for View. And The model class is for all. (like a global meaning...? for DB,view and so on)

Model和ViewModel类之间有什么区别?我可以找到解决这个问题的好方法吗?

What is different between Model and ViewModel class? May I get some a nice way to solve this?

推荐答案

顾名思义,视图模型非常特定于视图.它将是一个简单的POCO,仅包含视图所需的那些属性.

As the name says, view model is very specific to the view.It will be a simple POCO with only those properties needed for the view.

您的其他模型类是您的实体模型.因此,如果您使用的是EF代码优先方法,则需要EF将根据其生成数据库表的实体类定义.因此,基本上,这些实体类看起来与您的db模式结构非常相似.

Your other model class is your entity models. So if you are using EF code first approach, you need entity class definitions from which EF will generate the database tables. So basically these entity classes look very similar to your db schema structure.

通过创建视图模型,您将消除实体类与UI层的牢固耦合.现在您的UI层独立于您的实体类,并且如果您决定将数据访问代码从EF更改为其他内容,则完全不需要触摸视图,您只需要更新映射部分(从视图数据访问/服务层实体的模型)

By creating a view model, you are removing the strong coupling of your entity classes to the UI layer. Now your UI layer is independent of your entity classes and if you ever decide to change the data access code from EF to something else, you do not need to touch the views at all.You simply need to update the mapping part(from the view model to the data access/service layer entities)

视图模型有时看起来与您的实体模型非常相似,尤其是当您的实体模型是简单的表/类时.

View models sometimes looks very similar to your entity models, especially if your entity model is a simple table/class.

在您的情况下,由于视图要传递用户名和密码,因此您需要一个仅具有这两个属性的简单视图模型.当用户提交表单时,您可以读取值并根据需要使用它来构建域实体类对象.

In your case, since your view is passing a userid and password, you need a simple view model which has only those 2 properties. When user submits the form,you can read the values and use it to build an domain entity class object as needed.

public class LoginViewModel
{
  public string UserId { set;get;}
  public string Password  { set;get; }
}

您可以在视图模型中使用数据注释.MVC模型验证框架通过这些数据注释来进行验证.例如,由于用户应输入用户名和密码,因此可以使用适当的注释来修饰它们.

You can use data annotations with the view models. The MVC model validation framework these data annotations to do the validations. For example, since user should enter a UserId and Password, you may decorate them with appropriate annotations.

public class LoginViewModel
{
   [Required]
   public string UserId { set;get;}

   [Required]
   public string Password  { set;get; }
}

定义实体类时, [Key] 属性更有用.因此,我认为视图模型不需要它.请记住,视图模型更像是一个UI问题.它根本不了解您的基础数据存储机制.

The [Key] attribute is more useful when you define an entity class. So i would not think it is needed for a view model. Remember view model is more like a UI concern. It has no idea about your underlying data storage mechanism at all.

具有视图模型属性的一些最常用的属性是

Some of the most used attributes with view model properties are

  1. 必填
  2. MinLength
  3. 范围
  4. 网址
  5. 电话
  6. StringLength
  7. DataType
  1. Required
  2. MinLength
  3. Range
  4. Url
  5. Phone
  6. StringLength
  7. DataType

这篇关于ASP.NET Core MVC中的Model和ViewModel有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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