简体   繁体   English

使用MetadataType时,显示名称属性将不起作用

[英]Display name attribute won't work when use MetadataType

I'm Working with ASP.NET MVC 5, database first. 我正在使用ASP.NET MVC 5,数据库优先。 But entities in the edmx file, I can't add attributes, as that file will be overwritten every time I update it with database changes. 但是edmx文件中的实体,我无法添加属性,因为每次我使用数据库更改更新它时都会覆盖该文件。 I used MetadataType but Display name attribute won't work 我使用MetadataType但显示名称属性不起作用

namespace TemplateSite.Mvc.Models
{
    using System;
    using System.Collections.Generic;

    public partial class ezlib_BienMuc
    {
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
        public ezlib_BienMuc()
        {
            this.ezlib_bienMuc_tacGia = new HashSet<ezlib_bienMuc_tacGia>();
            this.ezlib_kiem_ke = new HashSet<ezlib_kiem_ke>();
        }

        public int Id { get; set; }
        public string ten_khac { get; set; }
        public string ky_hieu { get; set; }
        public string ten_vtat { get; set; }
        public string dvt { get; set; }
        public string anh_bia { get; set; }
        public string noi_dung { get; set; }
        public string tt_trach_nhiem { get; set; }
        public string tu_khoa { get; set; }
        public string de_muc { get; set; }
        public int ma_ngon_ngu { get; set; }
        public string noi_xuat_ban { get; set; }
        public int nam_xuat_ban { get; set; }
        public int ma_nha_xb { get; set; }
        public int lan_xuat_ban { get; set; }
        public string tung_thu { get; set; }
        public int tap_so { get; set; }
        public int ma_cap_hoc { get; set; }
        public int ma_khoi_hoc { get; set; }
        public int ma_mon_hoc { get; set; }
        public int ma_linh_vuc { get; set; }
        public int ma_dinhdang { get; set; }
        public int ma_size { get; set; }
        public string minh_hoa { get; set; }
        public string tai_lieu_nghe_nhin { get; set; }
        public int so_trang { get; set; }
        public string ghi_chu { get; set; }
        public string ghi_chu0 { get; set; }
        public string ghi_chu1 { get; set; }
        public string ghi_chu2 { get; set; }
        public Nullable<bool> active { get; set; }
        public string so_isbn { get; set; }
        public int ma_ddc { get; set; }
        public System.Guid sysuser_id { get; set; }
        public Nullable<System.DateTime> edit_date { get; set; }

        public virtual ezlib_dm_CapHoc ezlib_dm_CapHoc { get; set; }
        public virtual ezlib_dm_MaDDC ezlib_dm_MaDDC { get; set; }
        public virtual ezlib_dm_DinhDang ezlib_dm_DinhDang { get; set; }
        public virtual ezlib_dm_KhoiHoc ezlib_dm_KhoiHoc { get; set; }
        public virtual ezlib_dm_LinhVuc ezlib_dm_LinhVuc { get; set; }
        public virtual ezlib_dm_MonHoc ezlib_dm_MonHoc { get; set; }
        public virtual ezlib_dm_NgonNgu ezlib_dm_NgonNgu { get; set; }
        public virtual ezlib_nhap_an_pham ezlib_nhap_an_pham { get; set; }
        public virtual ezlib_dm_NXB ezlib_dm_NXB { get; set; }
        public virtual ezlib_dm_Size ezlib_dm_Size { get; set; }
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
        public virtual ICollection<ezlib_bienMuc_tacGia> ezlib_bienMuc_tacGia { get; set; }
        public virtual sysuser sysuser { get; set; }
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
        public virtual ICollection<ezlib_kiem_ke> ezlib_kiem_ke { get; set; }
    }
}

Here is the metadata 这是元数据

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;
using TemplateSite.Mvc.Classes;

namespace TemplateSite.Mvc.Models
{
    [MetadataType(typeof (BienMucMetaData))]
    public partial class ezlib_BienMuc
    {
    }

    public class BienMucMetaData
    {

        public int Id { get; set; }

        [Display(Name = "Tên khác")]
        public string ten_khac { get; set; }

        [Display(Name = "Kí hiệu")]
        [ReadOnly(true)]
        public string ky_hieu { get; set; }

        [Required(ErrorMessage = "Vui lòng nhập nhan đề mã hoá")]
        [Display(Name = "Mã hoá nhan đề (Tên viết tắt)")]
        public string ten_vtat { get; set; }

        public string dvt { get; set; }

        [Display(Name = "Tóm tắt nội dung (520$a)")]
        public string noi_dung { get; set; }

        [Display(Name = "Thông tin trách nhiệm (245$c)")]
        public string tt_trach_nhiem { get; set; }

        [Display(Name = "Từ khoá (653$a)")]

        public string tu_khoa { get; set; }

        [Display(Name = "Chủ đề - Đề mục (650$a)")]
        public string de_muc { get; set; }

        public int ma_ngon_ngu { get; set; }

        [Display(Name = "Nơi xuất bản (260$a)")]
        [Required(ErrorMessage = "Vui lòng nhập nơi xuất bản")]
        public string noi_xuat_ban { get; set; }

        [Display(Name = "Năm xuất bản (260$b)")]
        [YearValidate(Minimum = 1900, ErrorMessage = "Năm xuất bản phải từ năm 1900 đến năm hiện tại")]
        public int nam_xuat_ban { get; set; }

        public int ma_nha_xb { get; set; }

        [Display(Name = "Lần xuất bản (250$a)")]
        [Required(ErrorMessage = "Vui lòng nhập lần xuất bản")]
        [Range(1, int.MaxValue, ErrorMessage = "Vui lòng nhập chính xác lần xuất bản")]
        public int lan_xuat_ban { get; set; }

        [Display(Name = "Tùng thư (490$a)")]
        public string tung_thu { get; set; }

        [Display(Name = "Thông tin tập (254$n)")]
        [Required(ErrorMessage = "Vui lòng nhập thông tin tập")]
        [Range(1, int.MaxValue, ErrorMessage = "Vui lòng nhập chính xác thông tin tập")]
        public int tap_so { get; set; }

        public int ma_cap_hoc { get; set; }
        public int ma_khoi_hoc { get; set; }
        public int ma_mon_hoc { get; set; }
        public int ma_linh_vuc { get; set; }
        public int ma_dinhdang { get; set; }

        [Required(ErrorMessage = "Vui lòng nhập kích thước")]
        public int ma_size { get; set; }

        public string minh_hoa { get; set; }

        [Display(Name = "Tài liệu nghe nhìn kèm theo")]
        public string tai_lieu_nghe_nhin { get; set; }

        [Display(Name = "Số trang (500$a)")]
        [Required(ErrorMessage = "Vui lòng nhập số trang")]
        [Range(1, int.MaxValue, ErrorMessage = "Vui lòng nhập chính xác số trang")]
        public int so_trang { get; set; }

        [Display(Name = "Ghi chú")]
        public string ghi_chu { get; set; }

        public string ghi_chu0 { get; set; }
        public string ghi_chu1 { get; set; }
        public string ghi_chu2 { get; set; }

        [Display(Name = "Sử dụng")]
        public bool active { get; set; }

        [Display(Name = "Số ISBN (020$a)")]
        public string so_isbn { get; set; }

        public int ma_ddc { get; set; }
        public System.Guid sysuser_id { get; set; }
        public Nullable<System.DateTime> edit_date { get; set; }
        public virtual ezlib_dm_CapHoc ezlib_dm_CapHoc { get; set; }
        public virtual ezlib_dm_MaDDC ezlib_dm_MaDDC { get; set; }
        public virtual ezlib_dm_DinhDang ezlib_dm_DinhDang { get; set; }
        public virtual ezlib_dm_KhoiHoc ezlib_dm_KhoiHoc { get; set; }
        public virtual ezlib_dm_LinhVuc ezlib_dm_LinhVuc { get; set; }
        public virtual ezlib_dm_MonHoc ezlib_dm_MonHoc { get; set; }
        public virtual ezlib_dm_NgonNgu ezlib_dm_NgonNgu { get; set; }
        public virtual ezlib_nhap_an_pham ezlib_nhap_an_pham { get; set; }
        public virtual ezlib_dm_NXB ezlib_dm_NXB { get; set; }
        public virtual ezlib_dm_Size ezlib_dm_Size { get; set; }

        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage",
            "CA2227:CollectionPropertiesShouldBeReadOnly")]
        public virtual ICollection<ezlib_bienMuc_tacGia> ezlib_bienMuc_tacGia { get; set; }

        public virtual sysuser sysuser { get; set; }

        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage",
            "CA2227:CollectionPropertiesShouldBeReadOnly")]
        public virtual ICollection<ezlib_kiem_ke> ezlib_kiem_ke { get; set; }
    }
}

and here is the view 这是观点

        <ul class="form">
            <li>
                @Html.LabelFor(model => model.ten_khac, htmlAttributes: new {@class = "control-label col-md-21"})
                <div class="input">
                    @Html.EditorFor(model => model.ten_khac, new {htmlAttributes = new {@class = "form-control"}})
                    @Html.ValidationMessageFor(model => model.ten_khac, "", new {@class = "text-danger"})
                </div>
            </li>
        </ul>

Anyone know the problem please help me. 有人知道问题请帮帮我。 Thanks a lot 非常感谢

I try codes you posted and that just work completely fine here is what i got rendered 我尝试你发布的代码,这里完全正常工作是我得到的

<ul class="form">
   <li>
      <label class="control-label col-md-21" for="ten_khac">Tên khác</label>
            <div class="input">
                <input class="form-control text-box single-line" id="ten_khac" name="ten_khac" type="text" value="">
                <span class="field-validation-valid text-danger" data-valmsg-for="ten_khac" data-valmsg-replace="true"></span>
            </div>
        </li>
    </ul>

i think your problem is some thing else 我认为你的问题是另一回事

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 是否可以将 MetadataType 属性与接口一起使用:[MetadataType(GetType(iPerson))] - is it possible to use a MetadataType attribute with an interface: [MetadataType(GetType(iPerson))] 当edmx更改时,MetadataType属性将被删除 - MetadataType attribute removs when edmx change 如何在面向.net标准1.4的库中使用MetadataType属性 - How to use The MetadataType Attribute in a library targeting .net standard 1.4 Attribute.IsDefined没有看到使用MetadataType类应用的属性 - Attribute.IsDefined doesn't see attributes applied with MetadataType class 使用Validator时忽略.NET 4 RTM MetadataType属性 - .NET 4 RTM MetadataType attribute ignored when using Validator MetadataType和Attribute.IsDefined - MetadataType and Attribute.IsDefined 当我尝试将Metadatatype与viewmodel一起使用时,出现InvalidOperationException - I get InvalidOperationException when I try to use Metadatatype with a viewmodel OrmLite是否支持MetadataType属性? - Does OrmLite support MetadataType attribute? MetadataType无法识别IgnoreDataMemberAttribute - MetadataType doesn't recognize IgnoreDataMemberAttribute 导出到Excel时,如何将“显示名称”属性与GridView一起使用? - How can I use Display Name attribute with GridView when exporting to Excel?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM