相关疑难解决方法(0)

我可以使用数组或其他可变数量的参数初始化C#属性吗?

是否可以创建一个可以用可变数量的参数初始化的属性?

例如:

[MyCustomAttribute(new int[3,4,5])]  // this doesn't work
public MyClass ...
Run Code Online (Sandbox Code Playgroud)

c# attributes

97
推荐指数
5
解决办法
7万
查看次数

StringLengthAttribute如何工作?

在使用Entity Framework和ASP.NET MVC3验证我的模型时,我无法使用StringLengthAttribute.

我的模型基于实体框架实体,该实体具有部分类,该部分类使用MetadataType属性告诉MVC在搜索元数据时使用哪种类型.这显示在下面的代码中:

[MetadataType(typeof(PartMetadata))]
public partial class Part { }

class PartMetadata
{
    [DisplayName("Part number")]
    [Required(ErrorMessage="* Required")]
    [StringLength(50, MinimumLength = 3, ErrorMessage = "* Part numbers must be between 3 and 50 character in length.")]
    public string Number { get; set; }

    [StringLength(255, MinimumLength=3,
        ErrorMessage="* Part descriptions must be between 3 and 255 characters in length.")]
    public string Description { get; set; }

    [DisplayName("Drawing required?")]
    public bool DrawingRequired { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

我遇到的问题是描述字段未正确验证.使用下面的代码我的模型被验证为OK,即使描述字段留空,我也被重定向到我的控制器的索引页面.

if (ModelState.IsValid)
{
    return RedirectToAction("Index"); …
Run Code Online (Sandbox Code Playgroud)

c# asp.net validation attributes asp.net-mvc-3

15
推荐指数
1
解决办法
2万
查看次数

标签 统计

attributes ×2

c# ×2

asp.net ×1

asp.net-mvc-3 ×1

validation ×1