4

I am trying to write some client validation in a class library. I added a reference to System.Web.Mvc on my local machine here C:\Program Files (x86)\Microsoft ASP.NET\ASP.NET MVC 4\Assemblies\System.Web.Mvc.dll but I am getting the error

The type or namespace ModelClientValidationRule could not be found (are you missing a using directive or an assembly reference?)

I am targeting .Net Framework 4 and this MSDN link indicates that should be correct, so I am little stumped as the why the error is occurring.

I am using VS2013 and have .Net 4.5 on my local machine, and have written client validation in other projects. I expect that I could resolve this issue by adding the MVC NuGet package, but I would like to explore the alternatives.

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
ste-fu
  • 6,879
  • 3
  • 27
  • 46
  • Possible duplicate of [Build Errors - 'System.Web.Mvc.ModelClientValidationRule' Conflicts](http://stackoverflow.com/questions/8512021/build-errors-system-web-mvc-modelclientvalidationrule-conflicts) – Matt May 11 '16 at 10:11

2 Answers2

9

While ModelClientValidationRule is in the namespace System.Web.Mvc, it is in the assembly System.Web.WebPages, so you need to reference that assembly too.

Nils O
  • 1,321
  • 9
  • 19
  • For future reference: if you have a similar problem in the future look at the msdn page for the class you need it will name the assembly the class is in. – Nils O Apr 22 '15 at 21:00
  • Please view http://grandhah.blogspot.in/2015/07/add-custom-validation-condition-to-data.html I have explained the custom validation for data annotation – Sreerejith S S Jul 31 '15 at 09:53
  • @NilsO: It is not always that easy (I wish it was) - in my case, the error occurred after doing a NUGET update of MVC - although System.Web.MVC.dll was updated, ModelClientValidationRule was no longer available. Referencing the old DLL fixes the issue, but I want to use the new one. I am still looking for a solution - no luck in MSDN or via Mr.Google so far. Similar question (no answer yet): [here](http://stackoverflow.com/q/29208111/1016343) – Matt May 11 '16 at 08:46
0

In addition to Nils answer, you may need to add Microsoft.ASP.NET.MVC package from NuGet.

Amir
  • 1,722
  • 22
  • 20