I am using MVC3 with Razor. For input I have two types of control:
@Html.TextBoxFor
@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?
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.
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.
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.
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;
}
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;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With