是否可以创建一个可以用可变数量的参数初始化的属性?
例如:
[MyCustomAttribute(new int[3,4,5])] // this doesn't work
public MyClass ...
Run Code Online (Sandbox Code Playgroud) 在使用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)