在MVC3中采用数据库第一种方法,我的所有模型都被创建并存储在designer.cs
中在下面的代码中,我想强制NDC属性的正则表达式验证。输入需要类似于1234-1234-12或4位数字,短划线4位数,短划线2位数。
public partial class Drug : EntityObject
{
#region Factory Method
[EdmScalarPropertyAttribute(EntityKeyProperty=true, IsNullable=false)]
[DataMemberAttribute()]
public global::System.String NDC
{
[Required(ErrorMessage = "Please enter the Rx NDC")]
[RegularExpression(@"\d\d\d\d-\d\d\d\d-\d\d", ErrorMessage = "Please enter a correctly formatted NDC")]
get
{
return _NDC;
}
set
{
if (_NDC != value)
{
OnNDCChanging(value);
ReportPropertyChanging("NDC");
_NDC = StructuralObject.SetValidValue(value, false);
ReportPropertyChanged("NDC");
OnNDCChanged();
}
}
}I dont know how to apply the code above in the code below because I get the this error:
错误13属性“必需”在此声明类型上无效。它仅对'property,indexer,field,param'声明有效。 C:\ Users \ Daniel \ Desktop \ 320Final -Updated \ 320Final \ Models \ DBModel.Designer.cs
答案 0 :(得分:2)
您正在尝试设置属性内的属性:
public global::System.String NDC
{
[Required(ErrorMessage = "Please enter the Rx NDC")]
[RegularExpression(@"\d\d\d\d-\d\d\d\d-\d\d", ErrorMessage = "Please enter a correctly formatted NDC")]
get
{
return _NDC;
}
...
您需要在属性本身上设置它们:
[Required(ErrorMessage = "Please enter the Rx NDC")]
[RegularExpression(@"\d\d\d\d-\d\d\d\d-\d\d", ErrorMessage = "Please enter a correctly formatted NDC")]
public global::System.String NDC
{
get
{
return _NDC;
}
....