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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T19:45:12+00:00 2026-05-22T19:45:12+00:00

Im using asp.net mvc 2.0. I have the following HtmlHelper extension: AdminOnly(HtmlHelper helper, IPrincipal

  • 0

Im using asp.net mvc 2.0. I have the following HtmlHelper extension:

AdminOnly(HtmlHelper helper, IPrincipal User, string htmlToRender)
{
   //Render Html if have admin access.
}

I need to modify it to use in such a way:
AdminOnly(User).TextBoxFor(x=>x.MyField)
So that it could render edit field for MyField only if user has admin access.


For now Ive come out with the following solution:

AdminOnly(this MvcHtmlString resString, IPrincipal User)
{
   //Render Html if have admin access.
}

So in code I can write things like:

<%:Html.TextBoxFor(x=>x.MyProperty).AdminOnly(User)%>

It works but I would like to be able to add more inputs or more flexibility to add text before and after the inputs, like this:

<%:Html.PlainText("Set your age: ").TextBoxFor(x=>x.Age).AdminOnly(User)%>

or

<%: Html.AdminOnly("Set your age: ", User).AddTextBoxFor(x=>x.Age)%>
  • 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-22T19:45:13+00:00Added an answer on May 22, 2026 at 7:45 pm

    1) If you don’t expect rendering stuff for other than current identity and you are using Thread.CurrentPrincipal I’d recommend to leave out the user parameter and using HttpContext.Current.User or Thread.CurrentPrincipal.

    2) Using chained method calls would probably require quite a bit of work, instead I recommend taking advantage of lambda expressions.

    Extension:

    public static MvcHtmlString AdminOnly(this HtmlHelper htmlHelper, Func<MvcHtmlString> action)
    {
        if (HttpContext.Current.User.IsInRole("admin"))
            return action();
    
        return MvcHtmlString.Empty;
    }
    

    Usage:

    <%: Html.AdminOnly(() => Html.TextBoxFor(m => m.Field)) %>
    

    edit: updated for mvc 2

    • 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 am using ASP.NET MVC with jQuery. I have the following MVC Action that
I'm making an webpage using ASP.NET MVC. I have the following input hidden definied:
i am using asp.net mvc 2 here i have a following class Applicant and
i have the following code in c#. I'm using ASP.NET MVC 3. public override
I am using Ninject 2 with Asp.Net MVC 3. I have following module. public
I am in the start up of a project using ASP.NET MVC and have
I am currently creating an e-commerce site using C# ASP.NET MVC and have just
I am using asp.net MVC to develop an application that will have ajax interactions.
I've been developing a site using ASP.NET MVC, and have decided to use the

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.