Validación del modelo ASP.NET MVC 3

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.

preguntado el 08 de noviembre de 11 a las 16:11

If his users expect British English then it will be Organisation. If they expect American English then it will be Organization. From his consistent spelling, I expect it is the former. -

John Hartsock, Jared Peless, comments like this could have been useful on english.stackexchange.com but not very helpful in a programming related Q&A site such as StackOverflow. -

2 Respuestas

You could/DEBERÍA 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.

respondido 08 nov., 11:20

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; }

}

respondido 08 nov., 11:20

Thanks, answers the question. - Paul

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.