Enable client validation in Razor views (ASP MVC 3)
Asked Answered
M

5

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?

Morganmorgana answered 12/9, 2010 at 14:27 Comment(0)
A
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(); }
Arguelles answered 12/9, 2010 at 15:42 Comment(5)
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(...Morganmorgana
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 resultArguelles
Ok, I guess that works. I did not get my client side validation to fire, but that might be because of something else.Morganmorgana
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.Arguelles
Thx, I had forgotten the validation message. Works now!Morganmorgana
P
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>
Permeance answered 5/12, 2010 at 13:13 Comment(2)
btw ,you also should place jqueryvalidation js files in your view or your layout page.Permeance
Yes: NuGet jQuery.Validation.Unobtrusive and add <script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script> for both jquery.validate.min.js and jquery.validate.unobtrusive.min.js.Virgate
M
6

this tag

     @{ Html.EnableClientValidation(false); }

must come before the

  @using (Html.BeginForm())
Midway answered 2/5, 2011 at 16:7 Comment(0)
A
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.

Arson answered 4/1, 2011 at 16:51 Comment(0)
E
0

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

Make sure you use:

             <td>@Html.EditorFor(m => m.Email)</td>
Egon answered 5/5, 2014 at 23:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.