Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

@Html.ValidationMessageFor() custom validationMessage always shown

I am trying to use a different message for @Html.ValidationMessageFor() in ASP.NET MVC3. This works fine but it seems to make the message be always displayed , e.g. if I do this:

@Html.ValidationMessageFor(model => model.TimesheetEntry.Product) 

then the error is only shown when I submit the form and it is invalid. However if I do this:

@Html.ValidationMessageFor(model => model.TimesheetEntry.Product, "custom error") 

then that message is displayed as soon as I initially load the page. I'm probably doing something stupid here and any help would be appreciated.

like image 442
Tom Haigh Avatar asked Jun 10 '11 15:06

Tom Haigh


People also ask

What does HTML ValidationMessageFor do?

ValidationMessageFor<TModel,TProperty>(HtmlHelper<TModel>, Expression<Func<TModel,TProperty>>, String) Returns the HTML markup for a validation-error message for each data field that is represented by the specified expression, using the specified message.

How remove validation message after field is valid in MVC?

So, to clear the validation errors, you can do something like this to a form : var fieldContexts = form. __MVC_FormValidation. fields; for(i = 0; i < fieldContexts.

Which HTML helpers are used to show the validation messages?

ValidationMessage(HtmlHelper, String) Displays a validation message if an error exists for the specified field in the ModelStateDictionary object.


2 Answers

Have you tried the CSS from this question?

.field-validation-valid {     display: none; } .validation-summary-valid {     display: none; } 
like image 171
David Fox Avatar answered Sep 28 '22 07:09

David Fox


probably you have model parameter in your httpGet ActionResult method

like image 20
Amit Avatar answered Sep 28 '22 07:09

Amit