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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T11:52:27+00:00 2026-05-25T11:52:27+00:00

UPDATE #3: Entire question I have a class HB: public class HB { public

  • 0

UPDATE #3: Entire question

I have a class HB:

    public class HB
{
    public int Id { get; set; }

    [StringLength(3000)]
    public string Text { get; set; }
    public Title Title { get; set; }

}

And Title:

    public class Title
{
    public int Id { get; set; }
    public string Name { get; set; }
    public int MaxChar { get; set; }   
}

Before you can write a HB (which is kind of an article), you have to choose your title, so your StringLength for HB.Text can be determined. Meaning, this article can only have a certain amount of chars, deppending on what ‘Title’ the writer has. Example: Title1 can only write a ‘HB’ with 1000 chars, and Title2 can write a ‘HB’ with 3000 chars. So. Thats means the the StringLength has to come from Title.MaxChar. Whats the smartest way to do that?

The Title entity is prefixed data that will be stored in the db.

To be crystal clear, what I want to achieve is something in the line with: [StringLength(Title.MaxChar)]

Ive done structure/design for this mechanism in Webforms a million times, my brain just cant addapt to mvc, so some help would be appreciated. Code would be even more appreciated.

  • 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-05-25T11:52:28+00:00Added an answer on May 25, 2026 at 11:52 am

    Pretty sure that is not possible as written. This strikes me as trying to force business logic into the model that belongs in the controller.

    In this situation, I would make the attribute on the Text property [StringLength(3000)]. In the controller, during validation, I would write something along these lines:

    public ActionResult (HB model)
    {
        if (model.Text.Length > model.Title.MaxChar){
            ModelState.AddModelError("Text", string.Format("Text for this Title cannot exceed {0} characters.", model.Title.MaxChar));
        }
    
        if (ModelState.IsValid) 
        {
            //do stuff
            return RedirectToAction("Index"); //or something
        } 
        else 
        {
            return View(model);
        }
    }
    

    I believe this will accomplish what you are trying to do. Now, for the Title object, I’d flatten that out a bit in your model:

    public class HB
    {
        #region Base Properties
    
        public int Id { get; set; }
    
        [StringLength(3000)]
        public string Text { get; set; }
    
        #endregion
    
        #region Title Properties
    
        public int TitleId { get; set; }
        public string TitleName { get; set; }
        public int TitleMaxChar { get; set; }
    
        #endregion
    }
    

    This is assuming you need to display that information in your view. If you just need to reference it for your business logic validation, just have the TitleId property and use that to instantiate the Title object in your controller when you need it. Don’t forget to make hidden inputs for each of these properties if they are not editable!

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

Sidebar

Related Questions

Suppose I have a class like the following: public class Stage { public int
I edited my original text to demostrate my entire set of code for those
Sorry to be posting this question. :( But I have spent almost the entire
This code should update the entire table by applying a filter to its name
Original Question mysql-server-6.0.10 I have this problem, I'm using the COMPRESS function to update
Update : Gutted entire question with more thorough description Ok same question with different
Update: Check out this follow-up question: Gem Update on Windows - is it broken?
Update: Thanks for the suggestions guys. After further research, I’ve reformulated the question here:
Update: Please read this question in the context of design principles, elegance, expression of
Good morning, I have a question regarding default values and NULL values in Sybase

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.