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

  • Home
  • SEARCH
  • 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 7029953
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T00:36:22+00:00 2026-05-28T00:36:22+00:00

I have an extension generic method public static void AddError<TModel>( this ModelStateDictionary modelState, Expression<Func<TModel,

  • 0

I have an extension generic method

public static void AddError<TModel>(
    this ModelStateDictionary modelState, 
    Expression<Func<TModel, object>> expression, 
    string resourceKey, 
    string defaultValue)
{
    // How can I get a reference to TModel object from expression here?
}

I need to get the reference to TModel object from expression.
This method called by the following code:

ModelState.AddError<AccountLogOnModel>(
    x => x.Login, "resourceKey", "defaultValue")
  • 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-28T00:36:23+00:00Added an answer on May 28, 2026 at 12:36 am

    You cannot get to the TModel object itself without passing it into the method. The expression you are passing in is only saying “take this property from a TModel”. It isn’t actually providing a TModel to operate on. So, I would refactor the code to something like this:

    public static void AddError<TModel>(
        this ModelStateDictionary modelState, 
        TModel item,
        Expression<Func<TModel, object>> expression, 
        string resourceKey, 
        string defaultValue)
    {
        // TModel's instance is accessible through `item`.
    }
    

    Then your calling code would look something like this:

    ModelState.AddError<AccountLogOnModel>(
        currentAccountLogOnModel, x => x.Login, "resourceKey", "defaultValue")
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an extension method defined like so: public static TSource MaxBy<TSource, TResult>(this IEnumerable<TSource>
I have the following method: public static TEventInvocatorParameters Until <TEventInvocatorParameters, TEventArgs>(this TEventInvocatorParameters p, Func<TEventArgs,
I have an extension method here like this: public static class Extensions { public
I have the following method: public static string UlList(this HtmlHelper helper, List<IEntity> entities, string
I am making an extension. public static MvcHtmlString Image(this HtmlHelper helper, string src, object
I am having an issue using BinaryFormatter.Serialize. I have this generic extension method to
This extension method does not work on two separate development machines: public static string
I have an extension method I'm trying to make generic, for message passing. public
Let's say I have the following method: public static int CountNonNullMembers<T>(this IEnumerable<T> enumerable) {
I have an extension method on the HttpApplicationState object for getting my IoC container

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.