Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

@Html.TextBoxFor textbox isn't highlighted after failing validation

I am using MVC3 with Razor. For input I have two types of control:

  1. @Html.TextBoxFor
  2. @Html.TextAreaFor

Both have required field validation. @Html.TextAreaFor highlight the box if validation fails where as @Html.TextBoxFor does not.

Here is my code

HTML:

@Html.TextBoxFor(m => m.FirstName)

Model:

[Required(ErrorMessage = "First Name is required")]
public string FirstName { get; set; }

Why is the textbox created using @Html.TextBoxFor not hightlighted when its validation fails?

like image 698
user1148365 Avatar asked Jan 13 '12 20:01

user1148365


People also ask

What is the difference between using HTML TextboxFor and HTML TextBox?

IMO the main difference is that Textbox is not strongly typed. TextboxFor take a lambda as a parameter that tell the helper the with element of the model to use in a typed view. You can do the same things with both, but you should use typed views and TextboxFor when possible.

What is the difference between TextboxFor and Editorfor in MVC?

it's absolutly wrong answer, becuase the key difference is that Texbox returns input and editorfor returns your template where input is default template for editorfor.

How do I set TextboxFor to ReadOnly?

The TextBoxes can be made ReadOnly by setting the HTML ReadOnly attribute using the HtmlAttributes parameter in Html. TextBox and Html. TextBoxFor helper functions. Note: For beginners in ASP.Net MVC, please refer my article ASP.Net MVC Hello World Tutorial with Sample Program example.


2 Answers

Make sure the variables you're assigning to these input fields are [Required] in your model class.. Also did you strip out any css in a "clean up" process? Try adding this if it's not in there.

.field-validation-error
{
color: #ff0000;
}
.field-validation-valid
{
display: none;
}
.input-validation-error
{
border: 1px solid #ff0000;
background-color: #ffeeee;
}
.validation-summary-errors
{
font-weight: bold;
color: #ff0000;
}
.validation-summary-valid
{
display: none;
}
like image 113
jr3 Avatar answered Nov 15 '22 08:11

jr3


I had this problem myself. I added a new CSS class to the Site.css file (or whatever style sheet you prefer).

textarea.input-validation-error {
border: 1px solid #e80c4d;
}
like image 35
Auzi Avatar answered Nov 15 '22 07:11

Auzi