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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T05:14:55+00:00 2026-05-15T05:14:55+00:00

I am building a ASP.NET Mvc app. I have a Data model say User

  • 0

I am building a ASP.NET Mvc app. I have a Data model say User


public class user
{
    public int userId {get; private set};
    public string FirstName {get; set;}
}


The validation to be done is that the firstname cannot exceed 50 characters.

I have another presentation model in which i have the property FirstName too. I do not want to repeat the validation logic in both the models. I want to have it in one place and that should be it.

I can do it in a simpler way by adding a function which can be called while setting the property like


private string firstName;
public string FirstName
{
    get
    {
        return firstName;
    }
    set
    {
        if (PropertyValidator.ValidName(value)) // assuming ValidName exists and it will throw an exception if the value is not valid
        {
             firstName = value;
        }
    }
}


But I am looking for something much simpler so that I do not need to add this for every property I need to have it validated. I looked at ValidationAttribute but then again I can validate this only from a controller (ModelState.IsValid). Since this model could be used by some other type of apps like console app, I could not choose that. But if there is a way to use the Mvc's ModelState.IsValid from outside of a controller, that would be awesome. Any suggestions are greatly appreciated.

Thanks!!

  • 1 1 Answer
  • 2 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-05-15T05:14:55+00:00Added an answer on May 15, 2026 at 5:14 am

    You could take a look at FluentValidation. It is a great framework that allows you to separate the validation logic of your model from the model itself, provides easy way to unit test the validation logic and could be used in any type of application and of course seamlessly integrates with ASP.NET MVC.

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

Sidebar

Related Questions

I am building a ASP.NET MVC App that will allow a user to connect
I'm building a multi-tenant app with ASP.NET MVC and have a problem with validating
I'm building an asp.net MVC 2 app. I have a list view which lists
I have an ASP.NET MVC app that I have been building, and I am
I'm building an advanced search form for my ASP.NET MVC app. I have a
I am building my first ASP.Net MVC based app and have a problem accessing
I'm building an ASP.NET MVC 3 app and I've got a model that looks
I'm building a web app using EF Code First and ASP.NET MVC. I have
I have a ASP.NET MVC 2 app I am building and users are allowed
I'm building an ASP.NET MVC app and I'm using a repository to store and

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.