I'm working on a project using Visual Studio 2008 and have moved from the the MVC 2 Preview to RTM version. We would like to use model validation such as:
public class ViewModel
{
[Required(ErrorMessage="UserName is required.")]
[StringLength(10, ErrorMessage="UserName cannot be greater than 10 chars.")]
public string UserName { get; set开发者_JAVA百科; }
}
[HttpPost]
public ActionResult Register(ViewModel model)
{
if (ModelState.IsValid){} // Always true
}
However the ModelState.IsValid always returns true. I have a suspicion that it might be something to do with the version of System.ComponentModel.DataAnnotations.dll that we are referencing, currently version 99.0.0.0, which seems rather odd.
Does anyone know what version of this dll is included with the MVC 2 RTM for Visual Studio 2008?
I don't think System.ComponentModel.DataAnnotations
ships with MVC, it's part of the core .NET framework.
The dll file you should reference can be found in either:
.NET 3.5:
C:\Program Files\Reference Assemblies\Microsoft\Framework\.NETFramework\v3.5\Profile\Server Core\System.ComponentModel.DataAnnotations.dll
.NET 4:
C:\Program Files\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0\System.ComponentModel.DataAnnotations.dll
At least that's where they are on my computer!
HTHs,
Charles
Ps. If you are running a 64bit version of windows the directories will start with C:\Program Files (x86)
Turns out that System.ComponentModel.DataAnnotations.dll version 99.0.0.0 appeared to come from the 'Futures' version of MVC. We had used it so that we could use the Validator class.
Once I replaced the reference with the standard GAC version it all work. I think what I was seeing was the Input Validation of futures and what I was expecting was the late changes that made it into the RTM as documented by Brad Wilson.
Thanks for your help Charles
精彩评论