1

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.

1
  • I'm confused... can you reword this? Especially what you mean by passing this returned error data into either DataAnnotations or something else. It would be good if can show some code too. Commented Sep 16, 2010 at 20:50

1 Answer 1

2

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.
Sign up to request clarification or add additional context in comments.

1 Comment

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.