40

I try to add client side validation using this line of code:

@Html.EnableClientValidation()

But I keep getting this error message:

Compiler Error Message: CS1502: The best overloaded method match for 'Microsoft.WebPages.WebPageUltimateBase.Write(Microsoft.WebPages.Helpers.HelperResult)' has some invalid arguments

Is this working for anyone else, or is it another approach for this in ASP MVC 3?

Martin at Mennt
  • 5,677
  • 13
  • 61
  • 89

5 Answers5

78

You can, instead, use the following in place of the expected line of code.

@(ViewContext.ClientValidationEnabled = true)

Probably an oversight in the extension methods for htmlhelper.

Actually, you can use the HtmlHelper method by doing the following

@{ Html.EnableClientValidation(); }
Buildstarted
  • 26,529
  • 10
  • 84
  • 95
  • Thank you for your response, but when I put your line of code into the view all it does is output `True` into the page. Do I have to place the code anywhere special? Now im placing it just above my `@using(Ajax.BeginForm(...` – Martin at Mennt Sep 12 '10 at 16:09
  • 10
    The reason it doesn't work with just @ is because @ expects to write something to the browser but EnableClientValidation doesn't return anything so Microsoft.WebPages.WebPageUltimateBase.Write fails as a result – Buildstarted Sep 12 '10 at 17:22
  • Ok, I guess that works. I did not get my client side validation to fire, but that might be because of something else. – Martin at Mennt Sep 12 '10 at 18:46
  • 1
    Hmm...well, first I say check the source of your page to be sure the validation JS is being output. (I'm sure it is but it's always something to check) Then make sure you have @Html.ValidationMessageFor(m => m.Property) so the validation message knows where to go. – Buildstarted Sep 12 '10 at 19:22
  • Thx, I had forgotten the validation message. Works now! – Martin at Mennt Sep 14 '10 at 13:40
69

Hey, in ASP.NET MVC3, there's no need to add Html.EnableClientValidation() in view page, instead, just enable the clientValidation in the webconfig file as below:

<appSettings>
  <add key="ClientValidationEnabled" value="true" />
  <add key="UnobtrusiveJavaScriptEnabled" value="true" />
</appSettings>
Luke Puplett
  • 42,091
  • 47
  • 181
  • 266
bearing09
  • 739
  • 5
  • 5
  • 10
    btw ,you also should place jqueryvalidation js files in your view or your layout page. – bearing09 Dec 05 '10 at 13:18
  • 8
    Yes: NuGet [jQuery.Validation.Unobtrusive](http://nuget.org/packages/jQuery.Validation.Unobtrusive) and add `` for both `jquery.validate.min.js` and `jquery.validate.unobtrusive.min.js`. – Joel Purra Mar 26 '12 at 13:32
6

this tag

     @{ Html.EnableClientValidation(false); }

must come before the

  @using (Html.BeginForm())
marcelo
  • 61
  • 1
  • 1
4

Are you using the html <form> element on your page instead of Html.BeginFormto create your html FORM.

I had this exact same problem and worked out it was because the i was not using Html.BeginForm to create my FORM resulting in the required input attributes data-val-required="The Email field is required." data-val="true" class="input-validation-error and the place holder for the validation was not being injected into the page even though i had the @Html.ValidationMessageFor(m => m.User.Role) inserted on my view page.

Simon
  • 415
  • 1
  • 4
  • 15
0

In my case, I was not using EditorFor, but TextBoxFor!

Make sure you use:

             <td>@Html.EditorFor(m => m.Email)</td>
MaurGi
  • 1,698
  • 2
  • 18
  • 28