Reputation: 1104
Taking the database first approach in MVC3 all of my models are created and stored in a designer.cs
In the code below, I want to force a regex validation of the NDC property. The input needs to resemble 1234-1234-12 or 4 digits a dash 4 digits a dash 2 digits.
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:
Error 13 Attribute 'Required' is not valid on this declaration type. It is only valid on 'property, indexer, field, param' declarations. C:\Users\Daniel\Desktop\320Final -Updated\320Final\Models\DBModel.Designer.cs
Upvotes: 1
Views: 706
Reputation: 218828
You're trying to set the attributes inside of the property:
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;
}
...
You need to set them on the property itself:
[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;
}
....
Upvotes: 2