views:

35

answers:

1

Hi,

I have an ASP.Net MVC site, which connects to a web service.

The site's view model contains objects for each group of required service data AccountDetails (containing AccountId, AccountType, etc.), ContactDetails (containing Name, Address, etc.) and so on.

The service has a 'CreateUser()' method that accepts these objects as parameters, and it then performs all the validation itself - handing back an Object which has an array of any errors that have been found, including the name of the specific property/field.

I would like to know if there is a way of passing this returned error data into either DataAnnotations or something else.

I specifically can't write the conditions in the model itself, because the validation conditions within the web service are open to change at any moment - and we want this to dictate what fails and what succeeds.

== FURTHER INFO FOR MAKE IT A BIT CLEARER ==

Imagine I were locally (within the View Model) creating the ContactDetails class, I could very simply do this

public class ContactDetails
{
    [IsRequired()]
    [CustomAttributeofSomekind]
    public string FirstName { get; set; }

    public string LastName { get; set; }
}

However in this scenario - if we wanted to change the validation critera for whatever reason we would have to change it in both the web service AND in all the client websites that access the service.

We don't want to have to do this - instead I if (in the above) scenario ContactDetails.LastName is suddenly required and must be no more than 10 characters - this should only need updating in the web service.

+2  A: 

I think you have two options:

  1. Create a User class to wrap the CreateUser() method and add the DataAnnotations to that (this is what I would do, it allows you to go strongly-typed.)
  2. Call the CreateUser() method directly from the controller Action and use server-side validation. Add each validation error in the CreateUser() result to the ModelState.Errors collection when any validation rules are violated.
Dave Swersky
Thanks! I think for our purposes we'll go with ModelState.Errors as this ensures that we can leave all the validation rules enitrely with the service
beardtwizzle