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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T13:13:15+00:00 2026-06-11T13:13:15+00:00

I have a model like below: public class CreateStockcheckJobModel { [Engineer(true)] public EngineerModel Engineer

  • 0

I have a model like below:

public class CreateStockcheckJobModel
{
    [Engineer(true)]
    public EngineerModel Engineer { get; set; }
}

I’m rendering the Engineer property in a View<CreateStockcheckJobModel> using Html.EditorFor(m => m.Engineer, "EngineerEditor").

How do I access the value in the Engineer attribute (in this case true) from within the code in my partial view (EngineerEditor.ascx)?


Below is my editor code

<%@ Control Language="C#" Inherits="ViewUserControl<EngineerModel>" %>
<% if (PropertyImRenderingHasAttributeWithTrueBooleanValue) // What goes here?
   { %>
<p>Render one thing</p>
<% }
   else
   { %>
<p>Render another thing</p>
<% } %>

I’m aware of reflection, however i’m unsure how to use it as the attribute isn’t added to the EngineerModel class it’s added to the Engineer property of the CreateStockcheckJobModel class. If i could get the PropertyInfo that I’m rendering from the editor code then I’d be sorted, but I don’t know how to get that information. If I go down the route of enumerate all properties in the CreateStockcheckJobModel class then I’m going to get issues if I have more than one EngineerModel property (one might have the attribute with True, another might have False).

  • 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-11T13:13:16+00:00Added an answer on June 11, 2026 at 1:13 pm

    This could be done easily in ASP.NET MVC 3 and later by implementing the IMetadataAware interface on your custom EngineerAttribute:

    public class EngineerAttribute : Attribute, IMetadataAware
    {
        public EngineerAttribute(bool isFoo)
        {
            IsFoo = isFoo;
        }
    
        public bool IsFoo { get; private set; }
    
        public void OnMetadataCreated(ModelMetadata metadata)
        {
            metadata.AdditionalValues["IsFoo"] = IsFoo;
        }
    }
    

    and then inside the template:

    <%@ Control Language="C#" Inherits="ViewUserControl<EngineerModel>" %>
    <%
        var isFoo = (bool)ViewData.ModelMetadata.AdditionalValues["IsFoo"];
    %>
    <% if (isFoo) { %>
        <p>Render one thing</p>
    <% } else { %>
        <p>Render another thing</p>
    <% } %>
    

    Unfortunately this interface doesn’t exist in ASP.NET MVC 2. To achieve the same functionality you could write a custom metadata provider:

    public class MyMetadataProvider : DataAnnotationsModelMetadataProvider
    {
        protected override ModelMetadata CreateMetadata(
            IEnumerable<Attribute> attributes, 
            Type containerType, 
            Func<object> modelAccessor, 
            Type modelType, 
            string propertyName
        )
        {
            var metadata = base.CreateMetadata(attributes, containerType, modelAccessor, modelType, propertyName);
            var engineer = attributes.OfType<EngineerAttribute>().FirstOrDefault();
            if (engineer != null)
            {
                metadata.AdditionalValues["IsFoo"] = engineer.IsFoo;
            }
            return metadata;
        }
    }
    

    that you will register in your Application_Start in order to replace the default one:

    ModelMetadataProviders.Current = new MyMetadataProvider();
    

    And now you could access this metadata in your template the same way as I showed earlier, using ViewData.ModelMetadata.AdditionalValues["IsFoo"]. Obviously you could put an arbitrarily complex object inside the AdditionalValues property, not just booleans.

    Also you might find the following article useful about metadata.

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

Sidebar

Related Questions

I have a model called 'UserRoleHolder' like below. @Entity public class UserRoleHolder extends Model
I have a field in my model class like below: [ScaffoldColumn(false)] public DateTime DatePosted
I have the following model: public class Person { public int ID{get;set;} public string
i have a model say modela with a calculate field. like below class modelA
I have a Model which has some constants defined, like below: class Order(models.Model): WAITING
I have a model like this: class Users(db.Model): email = db.EmailProperty(required=True, indexed=True) user_name =
I have a model like the following one: public class TestModel{ public IList<Field> Fields
I have the following view model: public class MyViewModel { public bool SingleFamily {
Let's say I have an async controller like that below ~ public class HomeController
/// Model public class MenuModels { public Int32 MenuId { get; set; } public

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.