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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T16:41:44+00:00 2026-06-17T16:41:44+00:00

I am setting custom attributes to secure objects within a view. Here is the

  • 0

I am setting custom attributes to secure objects within a view. Here is the definition for my custom attributes:

[AttributeUsage(AttributeTargets.Property)]
public class SecureObjectAttribute : Attribute, IMetadataAware
{
    public void OnMetadataCreated(ModelMetadata metadata)
    {
        if(!metadata.AdditionalValues.ContainsKey("isSecure"))
        {
            if (ObjectId == 1)
            {
                metadata.AdditionalValues.Add("isSecure", true);
            }
            else
            {
                metadata.AdditionalValues.Add("isSecure", false);
            }
        }
    }
    public int ObjectId { get; set; }
}

Here is my View Model:

public class HomeViewModel
{
    [SecureObject(ObjectId = 1)]
    [Display(Name = "Name")]
    public string Name { get; set; }

    [Display(Name = "Address")]
    public string Address { get; set; }
}

I am defining helpers for Secure Labels and Secure TextBoxes. Here are my helper functions:

    public static MvcHtmlString SecureLabelFor<TModel, TProperty>(this HtmlHelper<TModel> html, Expression<Func<TModel, TProperty>> expression)
    {
        var metadata = ModelMetadata.FromLambdaExpression(expression, html.ViewData);
        if ((bool)metadata.AdditionalValues["isSecure"])
        {
            return null;
        }
        else
        {
            return html.LabelFor(expression);
        }
    }

    public static MvcHtmlString SecureTextBoxFor<TModel, TProperty>(this HtmlHelper<TModel> html, Expression<Func<TModel, TProperty>> expression)
    {
        var metadata = ModelMetadata.FromLambdaExpression(expression, html.ViewData);
        if ((bool)metadata.AdditionalValues["isSecure"])
        {
            return null;
        }
        else
        {
            return html.TextBoxFor(expression);
        }
    }

All of this works correctly, however, the problem that I am having is that every time I define a secure label and a secure textbox, the OnMetadataCreated method is invoked.

I am making a trip to the database on the OnMetadataCreated method (not shown in the example) to get permissions for the object, and I want to avoid duplicate trips for a single object.

The following is an example of code that is causing the OnMetadataCreated to be invoked twice in my View:


    @Html.SecureLabelFor(m => m.Name)
    @Html.SecureTextBoxFor(m => m.Name)

Any suggestions on how I can avoid a second call to the OnMetadataCreated method, or avoid a duplicate trip to the database?

  • 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-17T16:41:45+00:00Added an answer on June 17, 2026 at 4:41 pm

    You could store the result of the execution of the query in the HttpContext to avoid roundtrips to the database within the same request:

    public void OnMetadataCreated(ModelMetadata metadata)
    {
        // TODO: if you are using a DI framework you could pass the context
        // as a constructor argument dependency and use HttpContextBase
        HttpContext context = HttpContext.Current;
    
        bool isSecure;
        string key = "isSecure_" + ObjectId;
        if (context.Items.Contains(key))
        {
            // The isSecure value was found in the HttpContext => 
            // no need to query the database once again within this request
            isSecure = (bool)context.Items[key];
        }
        else
        {
            // Get the value from the database
            isSecure = GetFromDb(context.User.Identity.Name, ObjectId);
            // and store into the HttpContext to avoid roundtrips to the database
            // within this request
            context.Items[key] = isSecure;
        }
    
        ...
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have setup a custom provider to allow setting validation attributes from a data
I'm building a custom web control with a public property which I only want
Is there a MSBuild task for setting custom attribute in AssemblyInfo.cs? I found AssemblyInfo
I'm setting a custom StatusDescription in a RESTful WCF service when an exception is
I'm setting a custom banner and dialog image for my WiX generated MSI. <WixVariable
I'm having trouble setting a custom back button. Below is the current code I'm
I would like to read custom setting which i have done in ServiceConfiguration.cscfg &
I am having a requirement like to add a custom setting to android native
I have a application for android 2.3.3 where I am setting some custom titles
I'm setting up a custom blog that grabs the posts from a mysql database.

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.