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 9171113
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T16:07:08+00:00 2026-06-17T16:07:08+00:00

In my asp.net mvc 3 project I use [Required] attributes on properties that need

  • 0

In my asp.net mvc 3 project I use [Required] attributes on properties that need to be validated. Also I use ModelState.AddModelError to add errors to the same properties (with attributes). The errors added with the second approach doesn’t show up. What could be the reason?

Here is the code:

1) property with attibute

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

2) in controller action method

if (p.Name.Length < 3)
   ModelState.AddModelError("Name",
      "Product name should be at least 3 characters long");

3) in the view

@Html.EditorFor(i => i.CurrentItem.Product.Name)
        Html.ValidationMessageFor(i => i.CurrentItem.Product.Name)
        @Html.ValidationMessage("Name")

So, mabby I can’t use attributes and ModelState both?

  • 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-17T16:07:09+00:00Added an answer on June 17, 2026 at 4:07 pm

    You can achieve the same thing by adding a StringLength attribute:

    [StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 3)]
    

    Where 100 is the max length and 3 the minimum length.

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

Sidebar

Related Questions

I am trying to use HTML5 data- attributes in my ASP.NET MVC 1 project.
I use jQuery icons in my asp.net mvc project. Usually I use gray icons
I'm trying to use derobins wmd editor, with ASP.NET MVC 3 Project. I have
I have an ASP.NET MVC project with xVal and data annotations and I need
I know that I can use normal html files in ASP.NET MVC; however I
I use a .mdf database for my asp.net Mvc project. The project is on
I’m using ASP.NET MVC 3 for a project I have. The problem is that
I have an existing ASP.Net MVC project that is using entities / repositories in
I have a problem that I suppose comes from my ASP.NET MVC project using
I am working on an ASP.NET MVC 3 project and need to exclude certain

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.