Reputation: 3142
I have a model called Organisation, on the organisation I have a remote validation attribute:
[Required(ErrorMessage = "The organisation name is required")]
[Remote("NameCheck", "Manage", "Organisations", ErrorMessage="That organisation already exists")]
public string Name { get; set; }
This checks that the name of the organisation someone is adding doesn't already exists. If it does then they get an error message saying so.
I'm using a strongly typed view to render the organisation "edit" view. Because someone is editing, I don't want that remote validation to run because of course the organisation will exist.
Is there any way to achieve this? Basically, turn off the remote validation in some way when editing an organisation and have it turned on when creating an organisation.
Upvotes: 1
Views: 276
Reputation: 1039368
You could/SHOULD use different view models for the two views. So for example you will have CreateOrganizationViewModel and UpdateOrganizationViewModel. On the first view model the Name property will be decorated with the remote attribute whereas on the second view model it will not.
Upvotes: 4
Reputation: 86892
public class BaseOrganizationModel {
public int ID {get; set;}
}
public class UpdateOrganizationModel : BaseOrganizationModel {
[Required(ErrorMessage = "The organisation name is required")]
public string Name { get; set; }
}
public class InsertOrganizationModel : BaseOrganizationModel {
[Required(ErrorMessage = "The organisation name is required")]
[Remote("NameCheck", "Manage", "Organisations", ErrorMessage="That organisation already exists")]
public string Name { get; set; }
}
Upvotes: 3