public static string ProductHelper(this Product p) {
// Need to get the DisplayName value for p.Name property
}
Run Code Online (Sandbox Code Playgroud)
编辑:
[MetadataType(typeof(ProductMetadata))]
public partial class Product {
public class ProductMetadata {
[DisplayName("Product name")]
public object Name { get; set; }
}
}
Run Code Online (Sandbox Code Playgroud) 我使用 FluentValidation 框架向 MVC 项目中的模型添加验证和注释。
我需要将数据注释添加到模型的类级别。即,模型需要添加 DisplayColumn 属性。但是,由于我使用 FluentValidation(并且将应用程序的 ModelMetadataProvider 设置为使用 FluentValidation),即使我将 DisplayColumn 属性放在模型类上,它也不会被使用。但是,我找不到使用 FluentValidation 添加该注释的方法。
有谁知道我如何让它发挥作用?
谢谢
我正在使用DataAnnotations将验证应用于MVC ViewModel,它是几个实体框架对象和一些自定义逻辑的组合.已经为接口中的实体对象定义了验证,但是如何将此验证应用于ViewModel?
我最初的想法是将接口组合成一个并将组合接口应用于ViewModel,但这不起作用.这是一些示例代码,展示了我的意思:
// interfaces containing DataAnnotations implemented by entity framework classes
public interface IPerson
{
[Required]
[Display(Name = "First Name")]
string FirstName { get; set; }
[Required]
[Display(Name = "Last Name")]
string LastName { get; set; }
[Required]
int Age { get; set; }
}
public interface IAddress
{
[Required]
[Display(Name = "Street")]
string Street1 { get; set; }
[Display(Name = "")]
string Street2 { get; set; }
[Required]
string City { get; set; }
[Required]
string State { …Run Code Online (Sandbox Code Playgroud) 我正在尝试再次升级到AutoFixture 2,我遇到了对象上的数据注释问题.这是一个示例对象:
public class Bleh
{
[StringLength(255)]
public string Foo { get; set; }
public string Bar { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
我正在尝试创建一个匿名Bleh,但带有注释的属性将变为空,而不是使用匿名字符串填充.
[Test]
public void GetAll_HasContacts()
{
var fix = new Fixture();
var bleh = fix.CreateAnonymous<Bleh>();
Assert.That(bleh.Bar, Is.Not.Empty); // pass
Assert.That(bleh.Foo, Is.Not.Empty); // fail ?!
}
Run Code Online (Sandbox Code Playgroud)
根据Bonus Bits,StringLength应该从2.4.0开始支持,但即使它不受支持,我也不会期望一个空字符串.我正在使用NuGet的 v2.7.1 .我是否错过了创建数据注释对象所需的某种自定义或行为?
我打算实现一个自定义DisplayAttribute,以允许基于模型值的动态显示值,但我不能因为DisplayAttribute是密封的.
在我开始编写我自己的客户属性来模仿其行为之前DisplayAttribute,有人能想到为什么这个被密封了吗?我假设它背后有一个原因,如果是这样,那可能是我不应该试图通过滚动我自己"破解"这个限制的原因.
我不是要求任何人阅读微软的想法,我只是希望有人已经知道密封的设计理由,以便在滚动(或避免)我自己的实现时考虑到这一点.
我有一个看起来像这样的课程:
public class Analyst
{
[Column("Internal_ID")]
public int ID { get; set; } // if this is named like the column, it works
[Column("DisplayName")]
public string Name { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
这个对象就是这样填充的(下一部分是在一个继承自DbContext的类中):
public List<T> ExecProc<T>(string proc, Dictionary<string, object> params)
{
var parms = new List<SqlParameter>();
var sql = new StringBuilder();
sql.Append(sproc.StoredProcedure);
sql.Append(" "); // a space, that's all it is
foreach (var key in sproc.Parameters.Keys)
{
sql.Append(string.Format("{0},", key));
parms.Add(new SqlParameter(key, sproc.Parameters[key]));
}
if (sql[sql.Length - 1] == ',') // …Run Code Online (Sandbox Code Playgroud) 我正在构建“调度屏幕”,需要显示一个“时间”字段,以便用户输入调度的一天中的时间。
我不确定这是否是最佳选择,但我在该字段中使用了TimeSpan。为了验证输入,我想使用Range属性和DisplayFormat属性。
当我调试并输入看似有效的值时,Range属性指示超出范围的错误。谁能看到我在做什么错?TimeSpan是否适合此用法?任何帮助是极大的赞赏。
型号类别:
public class Schedule
{
public Schedule()
{
this.ScheduleTime = new TimeSpan(0, 0, 0);
}
/// <summary>
/// The time of day for the schedule to run
/// </summary>
[Required, DataType(System.ComponentModel.DataAnnotations.DataType.Time),
Display(Name = "Schedule Time", Description = "Number of Hours and Minutes after Midnight Central Timezone"),
DisplayFormat(DataFormatString = @"{0:hh\:mm\:ss}", ApplyFormatInEditMode = true),
Range(typeof(TimeSpan), "00:00", "23:59")]
public TimeSpan ScheduleTime { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
错误信息:

我的模型类中有一个属性用户名,我想对其进行验证以限制用户输入任何空格或逗号。目前,它仅使用以下正则表达式来限制空白,但我也想限制逗号。请建议
[Required]
[Display(Name = "UserName")]
[RegularExpression(@"^\S*$", ErrorMessage = "Username Cannot Have Spaces")]
public string UserName { get; set; }
Run Code Online (Sandbox Code Playgroud) 我想将VM_hostname,datetime和name属性作为Disk类的复合键。同时,Disk类的 VM_hostname和datetime 应该引用VirtualMachine类的VM_hostname和datetime (即外键)。
我这样做了,但是它给了我这个例外:类型'WebJob1.Historical.Disk'上属性'datetime'上的ForeignKeyAttribute无效。在依赖类型“ WebJob1.Historical.Disk”上找不到导航属性“ Datetime”。Name值应为有效的导航属性名称
有人知道吗?另外,请注意,即时消息使用数据注释。
public class VirtualMachine
{
[Key]
[Column(Order = 0)]
public string VM_Hostname { get; set; }
[Key]
[Column(Order = 1)]
public DateTime Datetime;
public virtual List<Disk> disks { get; set; }
}
public class Disk
{
[Key,ForeignKey("VirtualMachine"),Column(Order = 0)]
public string VM_hostname { get; set; }
[Key,ForeignKey("Datetime"), Column(Order = 1)]
public DateTime datetime { get; set; }
[Key, Column(Order = 2)]
public string name { get; set; } …Run Code Online (Sandbox Code Playgroud) 我从键值XML文件中读取以下方法。我传入了一个键,并返回了一个以前在视图中显示的值。
public static class TextManager
{
public static string GetValue(string key)
{
string returnVal = null;
XmlSerializer serializer = new XmlSerializer(typeof(Entries));
string path = HttpContext.Current.Server.MapPath("/App_Data/text-key-value.xml");
if (File.Exists(path))
{
Entries entries = (Entries)serializer.Deserialize(File.OpenRead(path));
var entry = entries.Where(u => u.Key == key).FirstOrDefault();
if (entry != null)
{
returnVal = entry.Value;
}
}
return returnVal;
}
}
Run Code Online (Sandbox Code Playgroud)
基本上,我希望能够在模型类中使用此方法作为数据注释,它将直接从站点文本文件中提取并设置为显示名称属性。
例如我要替换
[Display(Name = "Reference Code")]
public string ReferenceCode { get; set; }
Run Code Online (Sandbox Code Playgroud)
有了这个
[DisplaySiteText("ReferenceCodeKey")]
public string ReferenceCode { get; set; }
Run Code Online (Sandbox Code Playgroud)
DisplaySiteText会将字符串引用“ ReferenceCodeKey”传递给GetValue方法,将该引用归档在文件中,然后将标准显示名属性设置为文件中的内容。
我如何创建自己的自定义模型注释来实现此目的,过去我通过创建一个继承自ValidationAttribute的类来编写自定义验证注释,但在这种情况下我认为这不起作用。