Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# MaxLength属性取决于使用类_C#_Asp.net Mvc_Data Annotations_Maxlength - Fatal编程技术网

C# MaxLength属性取决于使用类

C# MaxLength属性取决于使用类,c#,asp.net-mvc,data-annotations,maxlength,C#,Asp.net Mvc,Data Annotations,Maxlength,如何在多个类使用的类上设置不同的MaxLength属性,其中每个使用类的MaxLength可能不同 在我的例子中,我使用的是实体框架和MVC。在双语字符串的实体数据模型中有一个复杂类型,它由一个英语字符串和一个法语字符串组成。我有很多实体都有双语字符串,它使用复杂类型将英语和法语映射到正确的数据库表/列。因此每个表有两列,但实体有一个类型为BilingualString的属性。单个表中的每个英语或法语字段长度始终相同,但每个表的字段长度可能不同 以下是我试图实现的一个简化示例: public p

如何在多个类使用的类上设置不同的MaxLength属性,其中每个使用类的MaxLength可能不同

在我的例子中,我使用的是实体框架和MVC。在双语字符串的实体数据模型中有一个复杂类型,它由一个英语字符串和一个法语字符串组成。我有很多实体都有双语字符串,它使用复杂类型将英语和法语映射到正确的数据库表/列。因此每个表有两列,但实体有一个类型为BilingualString的属性。单个表中的每个英语或法语字段长度始终相同,但每个表的字段长度可能不同

以下是我试图实现的一个简化示例:

public partial class BilingualString
{
    //[MaxLength(40)]  Cannot put MaxLength here because it would apply to all instances of BilingualString
    public string English { get; set; }
    public string French { get; set; }
}

public class ClassWithShortDescription
{
    //[MaxLength(20)] Cannot put MaxLength here because it does not makes sense.  It needs to be on each English and French properties.
    public BilingualString Description { get; set; }
}

public class ClassWithLongDescription
{
    //[MaxLength(200)] Cannot put MaxLength here because it does not makes sense.  It needs to be on each English and French properties.
    public BilingualString Description { get; set; }
}

由于英语和法语是在双语字符串中硬编码的,因此您还可以硬编码两个属性,而不是一个:

public class ClassWithShortDescription
{
    [MaxLengthForEnglish(20)]
    [MaxLengthForFrench(25)]
    public BilingualString Description { get; set; }
}

public class ClassWithLongDescription
{
    [MaxLengthForEnglish(200)]
    [MaxLengthForFrench(250)]
    public BilingualString Description { get; set; }
}

经过一些阅读和大量的谷歌搜索,我得出结论,根本的问题是由数据属性设置的元数据是静态的。因此,即使被不同的类使用,嵌套类属性也不能有不同的元数据。解决方案是将元数据放在消费类的属性上。创建应用于BilingualString类型属性的自定义maxlength属性

双语字符串的自定义MaxLength属性 公共类MaxLength双语字符串属性:MaxLengthAttribute { 公共MaxLengthBilingualStringAttribute(整型长度):基(长度) { }

    protected override ValidationResult IsValid(object value, ValidationContext validationContext)
    {
        BilingualString bilingualString = new BilingualString();
        if (value.GetType() == typeof(EdmBilingualStringVarCharSingleLine))
        {
            var bs = value as EdmBilingualStringVarCharSingleLine;
            bilingualString.English = bs.English;
            bilingualString.French = bs.French;
        }
        else
            return new ValidationResult("MaxLengthBilingualString Attribute does cannot be used with this type.");
        if (bilingualString.English != null && bilingualString.English.Length > this.Length )
            return new ValidationResult(string.Format("The maximum field length of {0} has been exceed for {1} English.", this.Length, validationContext.DisplayName));
        if (bilingualString.French != null && bilingualString.French.Length > this.Length)
            return new ValidationResult(string.Format("The maximum field length of {0} has been exceed for {1} French.", this.Length, validationContext.DisplayName));
        return ValidationResult.Success;
    }
}
实施示例:

public partial class BilingualString
{
    public string English { get; set; }
    public string French { get; set; }
}

public class ClassWithShortDescription
{
    [MaxLengthBilingualString(20)]
    public BilingualString Description { get; set; }
}

public class ClassWithLongDescription
{
    [MaxLengthBilingualString(200)]
    public BilingualString Description { get; set; }
}

有趣……那么MaxLengthForEnglishAttribute和French将是自定义属性,他们将如何设置正常的MaxLength属性?经过仔细检查,这没有任何意义。@RitchieD,说得对。你最终问到如何通过
描述
属性中的属性添加特定于语言的设置。给出了答案是一种解决方案,另一种是
[MaxLength(英语=200,法语=250)]
。如果两者都没有意义,我显然误解了你的问题。这两个
ClassWith….
类是否都源自
双语字符串?
?否。消费类是实体。是你的代码访问这些属性,还是它们被用作ASP.NET MVC模型状态验证的一部分?我使用的是正常的MVC模型状态valida你找到解决办法了吗?