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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T09:08:38+00:00 2026-05-19T09:08:38+00:00

So I have an extension method for the Html.CheckBoxFor() method that enables the user

  • 0

So I have an extension method for the Html.CheckBoxFor() method that enables the user to pass in an array of permissions like this:

<%= Html.CheckBoxForWithPermission(m => m.Current, new string[] { PERMISSIONS.hasICAdvanced }, new { @class = "economicTextBox", propertyName = "Current", onchange = "UseCurrent();UpdateField(this);" })%>

The method looks like this:

public static MvcHtmlString CheckBoxForWithPermission<TModel>(
                                                          this HtmlHelper<TModel> htmlHelper,
                                                          Expression<Func<TModel, bool>> expression,
                                                          string[] permissions,
                                                          object htmlAttributes
                                                         )
        {
            foreach (string permission in permissions)
            {
                if (Chatham.Web.UI.Extranet.SessionManager.PhysicalUser.IsInRole(permission))
                {
                    // the user has the permission => render the checkbox
                    return htmlHelper.CheckBoxFor(expression, htmlAttributes);
                }
            }
            // the user has no permission => render a readonly checkbox
            var mergedHtmlAttributes = new RouteValueDictionary(htmlAttributes);
            mergedHtmlAttributes["disabled"] = "disabled";
            return htmlHelper.CheckBoxFor(expression, mergedHtmlAttributes);
        }

Basically, I want to create the exact same thing except for an Html.TextBox method that we currently call like this:

<%= Html.TextBox("RateTimeStamp", Model.RateTimeStamp.HasValue ? Model.RateTimeStamp.Value.ToString("dd-MMM-yyyy") : "", new { @class = "economicTextBox", propertyName = "RateTimeStamp", onchange = "parseAndSetDt(this);", dataType = "Date" })%>

Since this helper is a little bit different I’m not really sure how to format the method.

Any help would be greatly appreciated. Thanks!

  • 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-19T09:08:39+00:00Added an answer on May 19, 2026 at 9:08 am
    public static MvcHtmlString TextBoxForWithPermission<TModel>(
        this HtmlHelper<TModel> htmlHelper,
        Expression<Func<TModel, bool>> expression,
        string[] permissions,
        object htmlAttributes
    )
    {
        foreach (string permission in permissions)
        {
            if (Chatham.Web.UI.Extranet.SessionManager.PhysicalUser.IsInRole(permission))
            {
                // the user has the permission => render the textbox
                return htmlHelper.TextBoxFor(expression, htmlAttributes);
            }
        }
    
        // the user has no permission => render a readonly checkbox
        var mergedHtmlAttributes = new RouteValueDictionary(htmlAttributes);
        mergedHtmlAttributes["disabled"] = "disabled";
        return htmlHelper.TextBoxFor(expression, mergedHtmlAttributes);
    }
    

    and then:

    <%= Html.TextBoxForWithPermission(
        x => x.RateTimeStamp, 
        new string[] { PERMISSIONS.hasICAdvanced }, 
        new { 
            @class = "economicTextBox", 
            propertyName = "RateTimeStamp", 
            onchange = "parseAndSetDt(this);", 
            dataType = "Date" 
        }
    ) %>
    

    and if you want to have the format you could use untyped helpers:

    public static MvcHtmlString TextBoxWithPermission<TModel>(
        this HtmlHelper<TModel> htmlHelper,
        string name,
        object value,
        string[] permissions,
        object htmlAttributes
    )
    {
        foreach (string permission in permissions)
        {
            if (Chatham.Web.UI.Extranet.SessionManager.PhysicalUser.IsInRole(permission))
            {
                // the user has the permission => render the textbox
                return htmlHelper.TextBox(name, value, htmlAttributes);
            }
        }
    
        // the user has no permission => render a readonly checkbox
        var mergedHtmlAttributes = new RouteValueDictionary(htmlAttributes);
        mergedHtmlAttributes["disabled"] = "disabled";
        return htmlHelper.TextBox(name, value, mergedHtmlAttributes);
    }
    

    and call like this:

    <%= Html.TextBoxWithPermission(
        "RateTimeStamp",
        Model.RateTimeStamp.HasValue 
            ? Model.RateTimeStamp.Value.ToString("dd-MMM-yyyy") 
            : "",
        new string[] { PERMISSIONS.hasICAdvanced }, 
        new { 
            @class = "economicTextBox", 
            propertyName = "RateTimeStamp", 
            onchange = "parseAndSetDt(this);", 
            dataType = "Date" 
        }
    ) %>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an HTMLHelper extension method that outputs HTML to Response.Write. How best to
I have a HtmlHelper extension method that I would like to apply some logic
I have an extension method to HtmlHelper: <%= Html.MyMethod( params )%> It works in
i have this code in one of my asp.net mvc views: <%Html.RenderFile(@C:\Members\newsletters\welcome.html);%> I have
I'm trying to use the Html.DropDownList extension method but can't figure out how to
So I am creating a HtmlHelper extension method and I have run into a
I've found a pattern in my Views like this: <% if (someCondition) { Response.Write(string.Format(Foo
I'm trying to create a strongly type html helper extension for a date picker
I have to build an xml output that represents a data structured for a
I have the following class declaration: public class EntityTag : BaseEntity, ITaggable I have

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.