ASP.NET MVC 2 Model encapsulated within ViewModel Validation?

I've been trying to make this work for some time. Based on the code you've included I think you're doing it right I think the problem is with the javascript validators that are supposed to be "emitted" into the page. There are several other questions here on SO relating to this problem and I don't think any of them offer a solution that works across the board.As far as I can tell, MVC2 RC is still broken UPDATE: If it works when you apply the attributes to the emitted code, take a look at this: stackoverflow.com/questions/2547132/when... It may be the MetadataType attribute that's not working.

Does it work if you try an interface as suggested in the question above? Also, another question: stackoverflow.com/questions/2475906/meta... That one is fairly recent and confirms that this is still a bug in MVC2.

I've been trying to make this work for some time. Based on the code you've included I think you're doing it right. I think the problem is with the javascript validators that are supposed to be "emitted" into the page.

There are several other questions here on SO relating to this problem and I don't think any of them offer a solution that works across the board. As far as I can tell, MVC2 RC is still broken. UPDATE: If it works when you apply the attributes to the emitted code, take a look at this: stackoverflow.com/questions/2547132/when... It may be the MetadataType attribute that's not working.

Does it work if you try an interface as suggested in the question above? Also, another question: stackoverflow.com/questions/2475906/meta... That one is fairly recent and confirms that this is still a bug in MVC2.

Ah, great. It works (not JS, but half is as good) if I apply the attributes directly on the LINQ-to-SQL generated code. Obviously not a solution.

– Program. X Apr 1 '10 at 14:39 Updated answer... – Dave Swersky Apr 1 '10 at 15:21 Thanks Dave. I'll have to wait till Wednesday to have a look as I'm off work for a bit.

Noted, though. – Program. X Apr 1 '10 at 17:45 I did try the interface method and it does sort of work.

Problem being that the MVC part couldn't create the type from the interface (obviously) and a few other issues I can't remember (sorry) right now. I chose to drop this as it didn't seem pure enough. The MetaDataType method defo doesn't work on encapsulated properties.My data structure (& prescribed requirements) is such that I can't break a 52-field table down into better components which would lend to better validation.

Hence having to hand-code for now. Thanks for your help, anyway. – Program.

X Apr 1 '10 at 8:45.

I am trying to get validation to work in ASP.NET MVC 2, but without much success. I have a complex class containing a large number of fields. (Don't ask - this is oneo f those real-world situations best practices can't touch) This would normally be my Model and is a LINQ-to-SQL generated class.

Because this is generated code, I have created a MetaData class as per http://davidhayden.com/blog/dave/archive/2009/08/10/AspNetMvc20BuddyClassesMetadataType.aspx. The problem is, the validation attributes on the metadata class are having no effect. I tried doing it via an Interface, but also no effect.

I'm beginning to think that the reason is because I am encapsulating my model within a ViewModel. As outlined in the comment, the ModelState. IsValid property always returns true, despite fields with the Validaiton annotations not being valid.

(Forenames being a key example).

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