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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T19:48:09+00:00 2026-06-18T19:48:09+00:00

Simple question really. I have MVC view that displays a Nullable Bool, e,g, Html.CheckBoxFor(model=>model.NullableBoolHere,

  • 0

Simple question really.

I have MVC view that displays a Nullable Bool, e,g,

Html.CheckBoxFor(model=>model.NullableBoolHere, Model.NullableBoolHere, 

and I want to create a new html helper, that will accept this type, and then convert

Null || False => False
True => True

so I have the following

public static MvcHtmlString CheckBoxFor<TModel>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, bool?>> expression, object htmlAttributes, bool disabled)
    {
        IDictionary<string, object> values = new RouteValueDictionary(htmlAttributes);

        if (disabled)
            values.Add("disabled", "true");

        Expression<Func<TModel, bool>> boolExpression = CONVERT_TO_BOOL_HERE(expression);


        return htmlHelper.CheckBoxFor(expression, values);
    }

Any help appreciated, I understand I will have to use recursion to copy the expression, but just not sure how to go about navigating the expression itself, find the bool?, convert to bool.

  • 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-18T19:48:11+00:00Added an answer on June 18, 2026 at 7:48 pm

    So, in the end, the only way I could find to do it was to resolve the bool? into a bool myself, then return a ‘normal’ checkbox by passing in the correct name etc.

    This does work a treat though, so all good. If you do know a better way of getting the correct ParameterName, it would be great to hear.

    public static MvcHtmlString CheckBoxFor<TModel>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, bool?>> expression, object htmlAttributes, bool disabled)
        {
            IDictionary<string, object> values = new RouteValueDictionary(htmlAttributes);
    
            if (disabled)
                values.Add("disabled", "true");
    
            //Compile the expression to get the value from it.
            var compiled = expression.Compile().Invoke(htmlHelper.ViewData.Model);
            bool checkValue = compiled.HasValue ? compiled.Value : false; //evaluate the compiled expression
    
            //Get the name of the id we should use
            //var parameterName = ((MemberExpression)expression.Body).Member.Name; // only gives the last part
            string parameterName = expression.Body.ToString().Replace("model.", "");//.Replace(".", HtmlHelper.IdAttributeDotReplacement);
    
            //Return our 'hand made' checkbox
            return htmlHelper.CheckBox(parameterName, checkValue, values);
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a question that has pretty much been asked here: asp.net mvc Html.ActionLink()
Pretty simple question really. I have two uint64 values. I want to divide the
A really simple shell script question. I have a file with something like this:
I have what is probably a really simple, studid question but I can't find
Really simple question: From within GWT I want to forward the user away from
This is a really simple question and I'm suprised I have to ask it
I have a really simple question. I got an ObservableCollection of some ViewModel (with
I'm building my first MVC project and I have a question about the model.
simple question really, i was wanting to know what naming conventions anybody puts on
Simple question really, is there any programmatic way to over load the choose a

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.