gpt4 book ai didi

c# - 如何将 MetadataType 属性与扩展类一起使用

转载 作者:行者123 更新时间:2023-11-30 17:08:30 24 4
gpt4 key购买 nike

我想将 DisplayAttribute 添加到 Client 实体(来自另一个项目),但不想用特定于 MVC 或 UI 的属性污染我的实体层。所以我计划通过将元数据类应用到 View 模型来添加 DisplayAttribute inheriting from the entity

如果我从 Client 实体继承,然后尝试使用 MetadataTypeAttribute 添加显示属性,它不会显示在浏览器中。有谁知道我如何实现分离以及能够将元数据添加到我的实体的功能?

Client 实体类:

public class Client
{
private string firstName;

private string lastName;

private string homeTelephone;

private string workTelephone;

private string mobileTelephone;

private string emailAddress;

private string notes;

public Title Title { get; set; }

public string FirstName
{
get { return this.firstName ?? string.Empty; }

set { this.firstName = value; }
}

public string LastName
{
get { return this.lastName ?? string.Empty; }

set { this.lastName = value; }
}

public string FullName
{
get
{
List<string> nameParts = new List<string>();

if (this.Title != Title.None)
{
nameParts.Add(this.Title.ToString());
}

if (this.FirstName.Length > 0)
{
nameParts.Add(this.FirstName.ToString());
}

if (this.LastName.Length > 0)
{
nameParts.Add(this.LastName.ToString());
}

return string.Join(" ", nameParts);
}
}

public Address Address { get; set; }

public string HomeTelephone
{
get { return this.homeTelephone ?? string.Empty; }

set { this.homeTelephone = value; }
}

public string WorkTelephone
{
get { return this.workTelephone ?? string.Empty; }

set { this.workTelephone = value; }
}

public string MobileTelephone
{
get { return this.mobileTelephone ?? string.Empty; }

set { this.mobileTelephone = value; }
}

public string EmailAddress
{
get { return this.emailAddress ?? string.Empty; }

set { this.emailAddress = value; }
}

public string Notes
{
get { return this.notes ?? string.Empty; }

set { this.notes = value; }
}

public Client()
{
this.Address = new Address();
}
}

ClientViewModel View 模型类:

[MetadataType(typeof(ClientMetaData))]
public class ClientViewModel : Client
{
internal class ClientMetaData
{
[Display(ResourceType = typeof(ResourceStrings), Name = "Client_FirstName_Label")]
public string FirstName { get; set; }
}
}

最佳答案

我认为您已将 typeof 参数更改为:

[MetadataType(typeof(ClientViewModel.ClientMetaData))]
public class ClientViewModel : Client
{
internal class ClientMetaData
{
[Display(ResourceType = typeof(ResourceStrings), Name = "Client_FirstName_Label")]
public string FirstName { get; set; }
}
}

关于c# - 如何将 MetadataType 属性与扩展类一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13723596/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com