views:

101

answers:

3

I've been chugging along OK with the use of data annotations made on buddy classes so far.

When it comes to a more complex view that requires a custom view model--one that includes a few select lists, for example...would I need to then transfer my validation attributes to the view model class?

I was planning to pass the full custom view model to populate an "Edit" view, but was hoping to just receive a simple object in my "Save" action.

Where are the flaws in this plan, or is the whole thing just one big pile of fail in the first place?

Thank you.

+1  A: 

Your view model will still inherit the validation from your base model.

ryan
Would it require a different call to determine the validity of the model?
PolishedTurd
no it shouldn't. why? are you experiancing difficulties?
griegs
I was guessing that it might not automatically validate without calling a specific validation on the sub-object.
PolishedTurd
+1  A: 

Don't know if this helps but I put my validation attributes against my model so that wherever i use the model i get the same validation. not ideal for some projects i realise.

actually, i put the attributes agains a partial class rather than my model because 90% of the time my model comes from a linq 2 sql file in my data repository

my controller then simply checks if the model is valid or not and the view does nothing except display data and errors really.

unsure if this is what you're asking though

griegs
+2  A: 

You're still validating that data that is ultimately going back into the database. So in order to keep your application DRY, you are best off to use the Buddy Classes for the original Model.

Edit
note: this doesn't exactly have anything to do with your question

I personally prefer extend the original Model for anything "Edit" related and I prefer to use a ViewModel for "Display" only (Details pages, List pages, etc).

Example: here's my buddy class, and in it I've added a "RegionName" property that I use in the Edit Page display, but it doesn't have anything to do with the database. You can do a similar thing with custom input data that you want to validate before manipulating it into "database usable" data. I use the RegionID in the database, but I prefer to use the friendly name for the visitor instead of the ID integer

<MetadataType(GetType(UserMetaData))> _ 
Partial Public Class User 
    Public RegionName As String 
End Class 

Public Class UserMetaData 

    <DisplayName("region name")> _ 
    <Required(ErrorMessage:="region name is required.")> _  
    Public Property RegionName As String 
End Class
rockinthesixstring
Thank you kindly.
PolishedTurd
----- GICH -----
rockinthesixstring