title: How can I collect model validation error messages?
body: <p>I am building an ASP.NET MVC application, and I am trying to find a way where I can collect data from the user in a custom view model, try and set these values to one or more of my entities, then based on validation logic on those entities, collect error messages if any and get them back to the view.  I am new to MVC and web design in general, it is therefore quite possible that I am making major conceptual errors, but I have tried to research as far as I could. </p>
  
  <p>I realize that this is more work than having the view be strongly typed to the entity, where it would then be easy to have the validation errors display, as in <a href="http://weblogs.asp.net/scottgu/archive/2010/01/15/asp-net-mvc-2-model-validation.aspx" rel="nofollow">this tutorial.</a>  However, I don't want to do this for security and because there are some places where I want to have values collected from a single view model to be set in multiple different entities.</p>
  
  <p>I also realize that I could set validation rules on the view model itself, rather then on the entity, but this seems like poor architecture, as I would have to define them redundantly in different view models, and I would then be less sure whether I had prevented bad values from being persisted to the database.</p>
  
  <p>My plan is therefore to have the validation rules be set on the entity itself and to have the view model as a dumb container. Then, in a different location in the application, I would apply the values from the view model to my entity(ies) in accordance my business logic. At this point, I would like my validation logic to be called. If the data is invalid, I plan on setting the error string in the custom attribute on the view model to the error from the validation logic on the entity. I am thinking it would go something like this: </p>
  
  <pre><code>public class CustomViewModel()
  {
      [SomeCustomValidation()] //Has a place for an error string and a boolean IsValid
      String Property { get; set; }
  }
  
  
  public class BusinessLogic()
  {
      CustomViewModel TestForValidity(CustomViewModel viewModel)
      {
  
          MyEntity.Property = viewModel.Property;
          // if(MyEntity.IsValid)?  catch SomeSortOfException?
          // collect error message, put it in the attribute on the view model, set IsValid to false
      }
  }
  
  public class MyEntity()
  {
      [MoreCustomValidation()]
      public String Property { get; set; }
  }
  </code></pre>
  
  <p>I therefore have three questions:</p>
  
  <ol>
  <li><p>When I try to pass data that does not satisfy my validation rules, will some sort of error or exception be thrown? Is there some indication I can use or collect when I try to pass in invalid data?</p></li>
  <li><p>If there is some error or exception thrown, how can I collect the error message so I can assign it to my view model?</p></li>
  <li><p>Most importantly, am I going about this all wrong? Can attributes not be modified at runtime, for example to include a new error message or to change IsValid to false? I know that I can use reflection to <a href="http://msdn.microsoft.com/en-us/library/z919e8tw%28v=vs.80%29.aspx" rel="nofollow">access the attributes</a>. If I can modify them, how would I do so?</p></li>
  </ol>
  
  <p>Thank you in advance for your help. I apologize if I misunderstand something big.</p>