ASP.Net MVC2 Validate two ViewModels of the same class differently using DataAnnotations?

Not sure what you mean by 'virtually duplicate and manually set each and every property manually in the original underlying class'. I've never liked the idea of buddy classes, and would personally recommend different view models for Admin and Public site (with appropriate validation set on each), and then mapping between the models using AutoMapper UPDATE: Regading Automapper, the basic usage is something like this: First you have to define your mappings. This lets automapper figure out in advance how to map objects.

You only need to do this once in the application, so a good place to do this in an ASP. NET app is in Application_Start() in Global.asax. For each pair of classes you want to map between, call: Mapper.CreateMap() Then, in your application code to do the map you just use: var destinationObject = Mapper.

Map(sourceOjbect).

Not sure what you mean by 'virtually duplicate and manually set each and every property manually in the original underlying class'. I've never liked the idea of buddy classes, and would personally recommend different view models for Admin and Public site (with appropriate validation set on each), and then mapping between the models using AutoMapper. UPDATE: Regading Automapper, the basic usage is something like this: First you have to define your mappings.

This lets automapper figure out in advance how to map objects. You only need to do this once in the application, so a good place to do this in an ASP. NET app is in Application_Start() in Global.asax.

For each pair of classes you want to map between, call: Mapper.CreateMap(); Then, in your application code to do the map you just use: var destinationObject = Mapper. Map(sourceOjbect).

I arrived at the same conclusion that AutoMapper needs to be involved, but I was hoping I was wrong. The problem occurs when say loading up an "Edit" View. You read the id parameter in and then go fetch the object that needs to be edited before passing it to the View.At that point, you need to somehow set the values inside the ViewModel based on the object you fetched.

This is where all paths lead to hell otherwise AutoMapper. I can make this entire architecture work perfectly well with two different validation rule sets, but setting the retrieved object's values for edit is where it blows. – Aaron Jan 8 at 11:14 But why don't you want to use automapper?

- it's perfect for exactly this scenario, simple to use and performant. You can almost forget it's there. – UpTheCreek Jan 8 at 11:18 I will probably resort to using it and I appreciate your very valid point, but I am very strict on trying to minimise the number of foreign/add-on components I bring into my systems.

From experience, 3 years down the track, suddenly ASP. Net MVC10 is released and your whole solution won't build because you're running a really old version of AutoMapper and whatever other add-on DLLs.. My first preference is always to try really hard to use native tools first before resorting to add-ons.As a general rule, the more add-ons, the less reliability and higher the future maintenance.. – Aaron Jan 8 at 11:22 I understand your concern, but there are some great tools out there now for . NET, especially useful for MVC (e.g. ORMs, IoC containers etc.) In my opinion you are just generating more work for yourself by not using them out of principle.

Stuff like Automapper has very focussed scope, and has no dependencies on high level stuff like MVC. Personally I'm more wary of using the MS stuff with the product lock-ins and closed source!(your Linq-to-SQL for example;) – UpTheCreek Jan 8 at 11:29 Ok I will take that on board and try to broaden my acceptance rate a little more with . Net add-ons.

But I have worked on some mammoth solutions from hell with way too many add-on components and in 5 years time when the original author has flown back to India, it can take you a week just to get it to build. Anyway, I've downloaded AutoMapper and will give it a go. Many thanks.

Cheers. – Aaron Jan 8 at 11:33.

Not sure what you mean by 'virtually duplicate and manually set each and every property manually in the original underlying class'. I've never liked the idea of buddy classes, and would personally recommend different view models for Admin and Public site (with appropriate validation set on each), and then mapping between the models using AutoMapper .

Stackoverflow.com/.../how-do-i-remain-dr... 7, 2010 – 2. How do I remain DRY with asp.net mvc view models & data ... The problem arises when you have complex views that require more than one object type. ... Use a unique view model class per action: The view model class only ... A good option would be to switch from DataAnnotations to Fluent Validation.

Andrewtwest.com/.../conditional-validati... 10, 2011 – Conditional Validation with Data Annotations in ASP.NET" rel="nofollow">ASP.NET MVC ... This is the view model I used for both partial views: public class CommentViewModel { . ... Since I'm using two partial views for the comment form (one for authenticated users and one for guests), I could use a dedicated view model for each. Stackoverflow.com/.../asp-net-mvc2-valid... 19, 2010 – NET MVC 2, the validation behaviour was changed in Release Candidate 2.

Aspx‎CachedSimilarJan 15, 2010 – NET MVC 2 that takes advantage of the new built-in DataAnnotation ... You could also implement the exact same scenario using VS 2008 and ASP. Uk/.../the-complete-guide-to-validation-in-asp.net-mv...‎CachedMar 10, 2011 – If you have not read part one yet, then please do take a look as it offers ... The class is decorated with an AttributeUsage attribute that tell . ... ASP.NET" rel="nofollow">ASP.NET MVC 2 had a mechanism for adding client side validation but it was not very pretty.

Odetocode.com/blogs/scott/archive/.../tw... 20, 2012 – Two ASP.NET" rel="nofollow">ASP.NET ... NET MVC 4 Fundamentals training course spends most of its time on new ... course is a start to finish introduction to programming with ASP. ... Models, view models, data annotations, and validation ... One question in module "Mass assignment", you demonstrate a ... Configuration class. Stackoverflow.com/.../how-do-i-remain-dr...0 16, 2012 – This became further useful as I could use data annotations to make cleaner use ... And since lots of other things use these domain models, not just this one MVC ... DRY, having multiple User-based classes with duplicate properties.

... that I am approaching ViewModel creation slightly differently these days.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions