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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T10:47:50+00:00 2026-06-05T10:47:50+00:00

I have created HtmlHelper in ASP.NET MVC 4 razor view engine C#. Can I

  • 0

I have created HtmlHelper in ASP.NET MVC 4 razor view engine C#.
Can I pass view model property to my helper class?
For example, I have property

[Required]
[Display(Name = "Your Lastname")]
public string Lastname { get; set; }

Can I pass this property to my helper something like this @Html.Example(model => model.Lastname) and then get data annotations in helper (if this field is required what is display name and etc.)?

  • 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-06-05T10:47:51+00:00Added an answer on June 5, 2026 at 10:47 am

    The [Display] attribute enriches the metadata. So you could fetch the information from the metadata.

    For example if you wanted to retrieve the display name inside the helper:

    public static class HtmlHelpers
    {
        public static IHtmlString Example<TModel, TProperty>(
            this HtmlHelper<TModel> html, 
            Expression<Func<TModel, TProperty>> ex
        )
        {
            var metadata = ModelMetadata.FromLambdaExpression(ex, html.ViewData);
            var displayName = metadata.DisplayName;
            return new HtmlString(html.Encode(displayName));
        }
    }
    

    and then if we assume that you have a view model:

    public class MyViewModel
    {
        [Required]
        [Display(Name = "Your Lastname")]
        public string Lastname { get; set; }
    }
    

    you could use the helper in your strongly typed view:

    @model MyViewModel
    
    @Html.Example(x => x.Lastname)
    

    Now let’s suppose that you wrote a custom metadata attribute:

    public class FooBarAttribute : Attribute, IMetadataAware
    {
        public FooBarAttribute(string bar)
        {
            Bar = bar;
        }
        public string Bar { get; private set; }
    
        public void OnMetadataCreated(ModelMetadata metadata)
        {
            metadata.AdditionalValues["foo"] = Bar;
        }
    }
    

    that you used to decorate your model with:

    public class MyViewModel
    {
        [Required]
        [FooBar("This is the bar")]
        public string SomeBar { get; set; }
    }
    

    and then inside your helper you could fetch the custom attribute:

    public static class HtmlHelpers
    {
        public static IHtmlString Example<TModel, TProperty>(
            this HtmlHelper<TModel> html, 
            Expression<Func<TModel, TProperty>> ex
        )
        {
            var metadata = ModelMetadata.FromLambdaExpression(ex, html.ViewData);
            if (metadata.AdditionalValues.ContainsKey("foo"))
            {
                var foo = metadata.AdditionalValues["foo"] as string;
                return new HtmlString(html.Encode(foo));
            }
            return MvcHtmlString.Empty;
        }
    }
    

    UPDATE:

    It seems that you need to fetch the Required message. No idea why you need to do this in a custom helper but here’s an example how you could achieve that:

    public static class HtmlHelpers
    {
        public static IHtmlString Example<TModel, TProperty>(
            this HtmlHelper<TModel> html, 
            Expression<Func<TModel, TProperty>> ex
        )
        {
            var me = (ex.Body as MemberExpression);
            if (me != null)
            {
                var required = me
                    .Member
                    .GetCustomAttributes(typeof(RequiredAttribute), false)
                    .Cast<RequiredAttribute>()
                    .FirstOrDefault();
                if (required != null)
                {
                    var msg = required.FormatErrorMessage(me.Member.Name);
                    return new HtmlString(html.Encode(msg));
                }
            }
            return MvcHtmlString.Empty;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using ASP.NET MVC 2 Preview 2 and have written a custom HtmlHelper
I have built a little repeater HtmlHelper for asp.net MVC and I would like
In our ASP.NET MVC project, we have an HtmlHelper extension method to generate a
I have created a basic site using ASP.NET routing according to Mike Ormond's example
i have this code in one of my asp.net mvc views: <%Html.RenderFile(@C:\Members\newsletters\welcome.html);%> I have
I have an ASP.Net MVC 3 web application and I am adding a check
Is their a solution to generate an email template using an ASP.NET MVC View
I'm interested in seeing what custom extensions other developers have created for the ASP.NET
I have an ASP.NET MVC project and I have a single action that accepts
I am trying to create my own helper function in ASP.NET MVC 3. Not

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.