在MVC 3数据库首先客户端验证 [英] Client Side validation in MVC 3 Database First

查看:102
本文介绍了在MVC 3数据库首先客户端验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我想补充 [必需] 在我的实体类,那么不显眼的审定工作正常。

If I add [Required] in my entity class then unobtrusive validation works fine.

[必需] 不加那里我第一次使用的数据库中生成我的实体类(* EDMX)。

[Required] is not added where I generate my entity class using database first(*.edmx).

如果我手动添加[必填]在我的实体属性,像

If I manually add [Required] in my entity property, something like

[Required]
public int {get;set;}

[必需] 时将更新我的EDMX删除。

[Required] will delete when I update my edmx.

所以我的问题是我怎么能执行客户端验证,如果我使用数据库首先在EF

So my question is how can I perform client side validation if I use database first in EF.

推荐答案

为您的实体分部类和使用的 MetadataType 的属性。见下面的例子:

Create a partial class for your entity and use the MetadataType attribute. See example below:

[MetadataType(typeof(MyEntity.Metadata))]
public partial class MyEntity
{
    private sealed class Metadata
    {
        [Required(ErrorMessage = "* required")]
        public string MyRequiredField { get; set; }
    }

    // Add other similar properties here...
}

这个类将是由设计师的变化产生的影响code。

This class will be unaffected by changes in the designer generated code.

这篇关于在MVC 3数据库首先客户端验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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