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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T14:21:17+00:00 2026-05-21T14:21:17+00:00

Could someone please clarify something for me. In my ASP.NET MVC 2 app, I’ve

  • 0

Could someone please clarify something for me. In my ASP.NET MVC 2 app, I’ve got a BaseViewModel class which includes the following method:

public virtual IDictionary<string, object> GetHtmlAttributes<TModel, TProperty>
                        (Expression<Func<TModel, TProperty>> propertyExpression)
{
    return new Dictionary<string, object>();
}

The idea being that each child viewmodel can override this method and provide a suitable set of html attributes, based on some logic, to be rendered in the view:

<%: Html.TextBoxFor(model => model.MyProperty, Model.GetHtmlAttributes
                                                 (model => model.MyProperty)) %>

However when used as in the line above, I get a compilation error when I hit the view:

The type arguments for method ‘...BaseViewModel.GetHtmlAttributes<TModel,TProperty> Expression<System.Func<TModel,TProperty>)‘ cannot be inferred from the usage. Try specifying the type arguments explicitly.

I have to do the following:

<%: Html.TextBoxFor(model => model.MyProperty, Model.GetHtmlAttributes
                             <ChildModel, string>(model => model.MyProperty)) %>

I’m just looking for some clarity as to how it tries to infer the type, it has no problem doing so in the HtmlHelper/TextBoxFor extension method?

Is it because HtmlHelper in the view will automatically be for the same type as is specified in the ViewUserControl at the top of the page, whereas my code can be for any type inheriting from BaseViewModel? Is is possible to write this in such a way that it can infer my model/property types?

  • 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-21T14:21:17+00:00Added an answer on May 21, 2026 at 2:21 pm

    In your example, the compiler has no way of knowing what type should TModel be. You could do something close to what you are probably trying to do with an extension method.

    static class ModelExtensions
    {
       public static IDictionary<string, object> GetHtmlAttributes<TModel, TProperty>
          (this TModel model, Expression<Func<TModel, TProperty>> propertyExpression)
       {
           return new Dictionary<string, object>();
       }
    }
    

    But you wouldn’t be able to have anything similar to virtual, I think.

    EDIT:

    Actually, you can do virtual, using self-referential generics:

    class ModelBase<TModel>
    {
        public virtual IDictionary<string, object> GetHtmlAttributes<TProperty>
            (Expression<Func<TModel, TProperty>> propertyExpression)
        {
            return new Dictionary<string, object>();
        }
    }
    
    class FooModel : ModelBase<FooModel>
    {
        public override IDictionary<string, object> GetHtmlAttributes<TProperty>
            (Expression<Func<FooModel, TProperty>> propertyExpression)
        {
            return new Dictionary<string, object> { { "foo", "bar" } };
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Could someone please clarify what the code means: public abstract class BaseJobProcessor<T> : JobProcessor
Could someone please tell me which objects types can be tested using Regular Expressions
I'm not able to understand the following multi-dimensional code. Could someone please clarify me?
Could someone please clarify for me the semantic difference between these two: self.foo =
Could someone please shed some light on the workings of A-sort (not asort()), which
Could someone please demystify interfaces for me or point me to some good examples?
Could someone please explain the best way to connect to an Interbase 7.1 database
Could someone please point me toward a cleaner method to generate a random enum
Could someone please explain? I couldn't find anything on the internet, everything talks about
Could someone please explain when would I want to use delegation instead of inheritance?

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.