Search code examples
linqvalidationasp.net-mvc-2client-side-validation

What can be causing Html.ValidateFor() method to produce a compile error?


I have view with the following which works:

<%= Html.TextBoxFor(m => m.FirstName, new { @class = "required_field_light" }) %>
<%= Html.ValidationMessageFor(m => m.FirstName) %>

However, if I change the ValidationMessageFor() to a ValidateFor() like this:

<%= Html.ValidateFor(m => m.FirstName) %>

I get this compile error:

"The best overloaded method match for 'System.IO.TextWriter.Write(char)' has some invalid arguments"
"Argument '1': cannot convert from 'void' to 'char'"

I assume I am missing something somewhere but I cannot figure out what it is. Has anyone else encountered this problem and found a solution, or does somebody have an idea how to resolve this?


Solution

  • Since ValidateFor() returns void, call it like so:

    <% Html.ValidateFor(m => m.FirstName); %>
    

    (Note no equal sign; addition of semicolon.)