Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8532029
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T09:37:12+00:00 2026-06-11T09:37:12+00:00

When we use mvc model validation engine, it automatically adds certain css classes from

  • 0

When we use mvc model validation engine, it automatically adds certain css classes from Site.css to editors, generated by HTML helper methods; i.e.one of these for certain object:

.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;
}

We can surely edit these styles to apply the required styling.

But is there an opportunity to tell the model validation engine, what exact style to use, depending on any user requirements, so that we could have one page with different fields, styled differently?

EDIT

Say we have the following view:

@model ModelValidation.Models.Appointment
@{
    ViewBag.Title = "Make a Booking";
}
<h2>
    Book an Appointment</h2>
@using (Html.BeginForm())
{
    @Html.ValidationSummary(true)
    <p>
        @Html.LabelFor(m => m.ClientName, "Your name: ") @Html.EditorFor(m => m.ClientName)
        @Html.ValidationMessageFor(m => m.ClientName)
    </p>        
    <p>
        @Html.LabelFor(m => m.Date, "Appointment Date: ") @Html.EditorFor(m => m.Date)
        @Html.ValidationMessageFor(m => m.Date)
    </p>
    <p>
        @Html.EditorFor(m => m.TermsAccepted) @Html.LabelFor(m => m.TermsAccepted, "I accept the terms & conditions")
        @Html.ValidationMessageFor(m => m.TermsAccepted, null, new { @class = "other-input-validation-error" })
    </p>
    <input type="submit" value="Make Booking" />
}

With @Html.ValidationMessageFor overload we can specify the css class to style error message. But how can we specify css class for the editor?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T09:37:13+00:00Added an answer on June 11, 2026 at 9:37 am

    I’m indeed waiting for other answers in impatience, but until that this is what I came up with.

    There is a necessary overload for concrete helper methods like Html.TextBoxFor or Html.PasswordFor, etc. So the following example shows what I wanted:

    @using (Html.BeginForm())
    {
        <p>
            @{ModelState state = ViewData.ModelState["ClientName"];            
            }
            @Html.LabelFor(m => m.ClientName, "Your name: ")
            @if (state != null && state.Errors.Count > 0)
            {
                @Html.TextBoxFor(m => m.ClientName, new { @class = "other-input-validation-error" })
            }
            else
            {
                @Html.EditorFor(m => m.ClientName)
            }
            @Html.ValidationMessageFor(m => m.ClientName)
        </p>        
    }
    

    But I am still open to any other ideas how to do this. I’m very new to asp in general and may be easily missing (simply don’t know) some nice feature of the technology.

    EDIT

    As it is said in Sanderson S. Freeman A. – Pro ASP.NET MVC 3 Framework Third Edition

    We can’t use the templated helpers to generate an editor for a
    property if we also want to provide additional attributes, so we have
    used the Html.TextBoxFor helper instead and used the version that
    accepts an anonymous type to use for HTML attributes.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an ASPNET MVC 2 project. When I use <%= Html.TextBoxFor(model => model.Login)
I use MVC 3 Model Validation Attributes and jquery unobtrusive to show validation error
I am creating a model binder to use with asp.net mvc. This is what
I have a Spring MVC application trying to use a rich domain model, with
in my view Inherits=System.Web.Mvc.ViewUSerControl<Model.Person> How can I use an interface to restrict what the
Current validation method for use in MVC 3 seems to be ValidationAttributes. I have
I'm trying to use unobstrusive validation with ASPNET MVC 3 project and placeHolder jQuery
I'm using Fluent Validation with the Ninject.Web.Mvc.FluentValidation library to automatically wire up all of
I am using Spring MVC 3 and I try to use the validation annotation
I use datetime picker for ASP.NET MVC with razor, when submitted form got validation

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.