0

I have a ASP.NET MVC application that makes calls to my ASP.NET Web API layer.

The Web API layer also uses EF 6.x (latest).

Now when e.g. say the MVC has a user form to create a new user, when the user hits submit I do:

  1. The form is backed by my UserDTO
  2. Pass the UserDTO as a parameter to my SaveUser web-api client.

Now in the Web API layer, if there is a validation error, how can I use attribute based validation and pass the error back to the MVC view page?

Update

I want to use ModelState.IsValid and if there are errors pass those back to the MVC layer.

3
  • just a question but wouldnt you just use attribute validation on your mvc viewmodel and handle it on submit before it goes to the web api layer? viewmodel with validation attributes -> pass validation -> map to dto -> web api layer. Commented Aug 28, 2014 at 19:16
  • @f0x Yes but in case things get out of sych the real validation has to be done at the API layer. Meaning the actual validation is at the API layer as the API developer might want to add additional validation. Commented Aug 28, 2014 at 19:39
  • fair enough - I would look into ValidationResult then imo. Commented Aug 28, 2014 at 20:41

1 Answer 1

0

create a error property in your UserDTO then return

public UserDTO saveuser(UserDTO user)
    if(validation error)
    {
    UserDto user = new UserDTO()
    User.error = [your error message]
    return user;
    }
else{
//////// save it and retuen null.
}

in your MVC

UserDTO user = saveuser();[your web api client]

then you handle it in the MVC.

1
  • I want to use the built-in attribute based validation, how can I to ModelState.IsValid and pass the error back. Commented Aug 28, 2014 at 19:07

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.