Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 634
Sailing Judo Avatar asked Jun 15 '10 16:06

Sailing Judo


2 Answers

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

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

(Note no equal sign; addition of semicolon.)

like image 181
Levi Avatar answered Nov 10 '22 10:11

Levi


For those of you using Razor, you can do the same with

@{ Html.ValidateFor(x => x.FirstName); }

instead of the usual

@Html.ValidateFor(x => x.FirstName)

Again, as was mentioned by Levi, because a ValidateFor returns void, not MvcHtmlString like most Html. methods. And on that note, having no clue about what you're doing, if you're trying to use Html.ValidateFor I'd bet that you actually want to use:

@Html.ValidationMessageFor(x => x.FirstName)
like image 6
Serj Sagan Avatar answered Nov 10 '22 10:11

Serj Sagan