Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Enable client validation in Razor views (ASP MVC 3)

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?

like image 541
Martin at Mennt Avatar asked Sep 12 '10 14:09

Martin at Mennt


People also ask

How do I make sure client validation is enabled in MVC?

We can enable and disable the client-side validation by setting the values of ClientValidationEnabled & UnobtrusiveJavaScriptEnabled keys true or false. This setting will be applied to application level. For client-side validation, the values of above both the keys must be true.

How can we implement client-side validation in ASP.NET MVC?

In the server-side validation, the page must be submitted via a postback to be validated on the server and if the model data is not valid then the server sends a response back to the client with client-side validation, the input data is checked as soon as they are submitted, so there is no postback to the server and ...

Where can client-side validation be deactivated globally for all Razor views?

You can simply disable client side validation in the Razor view prior to render the field and re-enable client side validation after the field has been rendered.


3 Answers

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(); } 
like image 137
Buildstarted Avatar answered Oct 13 '22 07:10

Buildstarted


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>
like image 21
bearing09 Avatar answered Oct 13 '22 09:10

bearing09


this tag

     @{ Html.EnableClientValidation(false); }

must come before the

  @using (Html.BeginForm())
like image 24
marcelo Avatar answered Oct 13 '22 09:10

marcelo