0
votes

I added validation on my Model, like this:

                [Required]
                [StringLength(60, MinimumLength = 4)]
                [Display(Name = "Users code")]
                public string UserCode { get; set; }

As you can see guys, when I'm creating new user, field UserCode must be filled, and minimum length of characters into the field is 4 and max is 60.

And here is how it looks when I try to skip this field or insert less than 4 characters.. enter image description here

As you can see guys there is a text The string must be a field bla bla bla, how could I hide or remove that? I really don't need it

3
Look on your .cshtml - remove @Html.ValidationMessageFor... - freedomn-m
You might also like to change @Html.ValidationSummary(true) to @Html.ValidationSummary(false) - freedomn-m
Why don't you want it? How would your users know its invalid if your don't tell them. - user3559349
@StephenMuecke as you can see input became red if something is wrong with validation, and that's reason I don't need message also, highlighting input to red is enought for now :) - Roxy'Pro
@freedomn-m that solved issue so you might post it as answer so I might accept it as you were fastest one - Roxy'Pro

3 Answers

1
votes

Validation text under inputs is added automatically by the MVC scaffolding and will have an entry in the .cshtml like:

@Html.ValidationMessageFor...

simply removing this line from the .cshtml will remove it from the output.


As a caveat: this will then mean the user won't know exactly what's wrong with the input. It could be that the field is required, or in the wrong format. In this case, a user code of 3 characters may appear to be correct, but they'll still get the error.

There should be some other mechanism for displaying the reason, either with a custom notification or eg changing the ValidationSummary to not exclude property errors (naughty Microsoft using negative terms requiring double negatives... tsk), eg change

@Html.ValidationSummary(true)

to

@Html.ValidationSummary(false)

BONUS

If you find yourself removing these manually, you can edit the Code Template t4 to remove them. Copy the edit.cs.t4 from program files (I'll leave you to find it) to a folder in your project with the same structure, specifically:

/CodeTemplates/MvcView/Edit.cs.t4

add change it as required. This will then be used when you scaffold an edit page.

1
votes

You should remove your error message of your field from .cshtml page :

@Html.ValidationMessageFor(a => a.UserCode, "", new { @class = "text-danger" })
0
votes

You can switch off client-side unobtrusive validation for a single field by adding a data-val='false' attribute:

@Html.TextBoxFor(m => m.BatchId, new { data_val = "false" })

This will override the data-val='true' attribute that MVC adds due to any System.ComponentModel.DataAnnotations attributes. The HTML element will still be decorated with other validation attributes (e.g. data-val-required) but they won't have any effect.

(Note the underscore in data_val above. MVC automatically converts underscores to hyphens in anonymous type properties, so data_val becomes data-val when rendering the HTML)