附加验证,在MVC控制器/视图中使用EF对象? [英] Attaching validation to EF objects used in MVC controllers/views?

查看:222
本文介绍了附加验证,在MVC控制器/视图中使用EF对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在一起扔一个快速项目(CRUD的形式),并决定跳过视图模型,并直接在控制器和视图中使用EF实体。因为我不习惯这种方法,我感到困惑的处理验证。

We're throwing together a quick project (CRUD forms) and decided to skip view models and use EF entities directly in controllers and views. Since I'm not used to this approach, I'm confused about handling validation.

例如:一个数据库字段有25的长度这是如何得到转移(如果可以),以在我看来,一个验证约束?如果我是使用中间的模型,我会属性附加到模型的属性和它的工作。我如何将直接做到这一点使用EF对象?谢谢。

For example: a DB field has a length of 25. How does that get transferred (if it can) to a validation constraint in my view? If i was using an intermediate model, I would attach attributes to the model properties and it would work. How would I do this using EF objects directly? Thanks.

推荐答案

这可以用在EF生成的类MetadataType属性来完成。该EF生成局部类。因此,那些可扩展和属性添加到它。然后另一个哥们级可写,可以有成员装饰。例如:

This can be done using MetadataType attribute on the Ef generated classes. The EF generates partial classes. So those can be extended and attribute added to it. Then another "buddy class" can be written that can have member decoration. For example

[MetadataType(typeof(EFGeneratedClass_MetaData))]
public partial class EFGeneratedClass
{
}

public partial class EFGeneratedClass_MetaData
{
    [Required]
    [Display(Name="Member1 Display")]
    public string Member1 {get; set;}
}

这篇关于附加验证,在MVC控制器/视图中使用EF对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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