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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T02:15:56+00:00 2026-05-25T02:15:56+00:00

The new helper methods EditorFor, TextBoxFor are very cool in avoiding hard coded field

  • 0

The new helper methods EditorFor, TextBoxFor are very cool in avoiding hard coded field names in views.

I’m writing validation functions where services validate complex model data.
If there is an error with a field the service should push an error for this field into the modelstate dictionary.

Is there an helper method with generates the complete model name?

EDIT:
Samplemodel:

class CustomModel {
  public InnerModel Inner { get; set; }
}
class InnerModel {
  public String Field1 { get; set; }
  public SomethingMoreToValidate[] More { get; set; }
}
class SomethingMoreToValidate {
  public int A { get; set; }
  public int B { get; set; }
}

The input field for InnerModel.SomethingMoreToValidate[0].A in the view has the name “InnerModel.SomethingMoreToValidate[0].A”. To bind the model state errors to the field, I have to put the errors with this name into the ModelStateDictionary.

In the View displaying the InnerModel data, I can write:

Html.EditorFor(m => m.SomethingMoreToValidate[0].A);

If a property is renamed, compiler warns me.
In the validating controller or service class I have to write:

ModelState.AddModelError(
  "InnerModel.SomethingMoreToValidate[0].A", 
  "There is a problem with this field");

If a property is renamed, there is no feedback if I have no other checks like unittests.
I would like to write:

ModelState.AddModelErrorFor(
  m => m.InnerModel.SomethingMoreToValidate[0].A, 
  "There is a problem with this field")
  • 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-25T02:15:57+00:00Added an answer on May 25, 2026 at 2:15 am

    You could write a helper using the GetExpressionText method:

    public static string GetExpressionText<TModel, TProperty>(Expression<Func<TModel, TProperty>> expression)
    {
        return ExpressionHelper.GetExpressionText(expression);
    }
    

    and then:

    var expression = GetExpressionText<MyViewModel, string>(
        x => x.InnerModel.SomethingMoreToValidate[0].A
    );
    
    ModelState.AddModelErrorFor(expression, "There is a problem with this field");
    

    This being said, you should obviously have unit tests in a properly written application.

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

Sidebar

Related Questions

.NET Framework 4 introduces new helper method Stream.CopyTo . It uses Read/Write methods and
I am pretty new to the zend framework and the JQuery helper. I have
public static string TimeLine2(this HtmlHelper helper, string myString2) { StringBuilder myString3 = new StringBuilder();
New to javascript/jquery and having a hard time with using this or $(this) to
here are two little helper methods I have made for downloading files. I have
Is it a good practice to place helper methods within the MasterPage? Should I
The Reactive Extensions come with a lot of helper methods for turning existing events
I'm looking for a way to set up some helper methods from within a
I'm in the process of writing a simple prototype in Node.js with some helper
How would I quickly debug helper methods in script\console. I'm talking about making changes

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.