马克场"只读"有数据注释 [英] Mark a field "Read Only" with Data Annotations

查看:206
本文介绍了马克场"只读"有数据注释的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图让 ID 字段只读。它是在DB,因此用户不会将其设置一个标识字段。不过,他们希望看到它。我缺少的下面,当分配给的DataForm 仍然允许进行编辑的值。

 公共类StatusChoice:BindableBase 
{
私人诠释身份证;

[键,DatabaseGenerated(DatabaseGeneratedOption.Identity)

公众诠释ID
{
获得{返回ID; }

{
ID =价值;
OnPropertyChanged();
}
}
}


解决方案

标记属性有只读属性。

  [只读(真)] 
公共小数BodyMassIndex {搞定;私人集; }

按照以下链接了解更多
具有用于改变asp.net MVC 3 DataAnnotations的行为?


I am trying to make the ID field read only. It is an Identity field in the DB so the user will not be setting it. However they would like to see it. What am I missing as the below, when assigned to a DataForm still allows that value to be Edited.

public class StatusChoice : BindableBase
{
    private int id;

    [Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    [Editable(false,AllowInitialValue = false)]
    public int ID
    {
        get { return id; }
        set
        {
            id = value;
            OnPropertyChanged();
        }
    }
}

解决方案

Mark Property with ReadOnly attribute.

[ReadOnly(true)]
public decimal BodyMassIndex { get; private set; }

Follow below link for more Has the behavior for DataAnnotations in asp.net mvc 3 changed?

这篇关于马克场"只读"有数据注释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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