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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T11:48:14+00:00 2026-06-17T11:48:14+00:00

I’m using ASP.NET MVC3 with Razor. I have a boolean value in my model

  • 0

I’m using ASP.NET MVC3 with Razor.

I have a boolean value in my model which I would like to render as a check box in my view. However, I would like the check box to indicate the reverse of the boolean state. In other words, selecting the check box should set the bound model object to false, not true, when the form is submitted. And vice versa.

I can do this to set the value attribute on the rendered check box input element:

@Html.CheckBoxFor(model => model.MyBoolean, new { value = "false" })

But the hidden input element that is automatically created still has a value of false. Thus they both have a value of false, which means the bound model object is always set to false.

So how can I force the HTML helper to set the hidden element to true and the check box element to false?

I know that (a) I could alter the model and the database, (b) I could alter the values with javascript just prior to submission, and (c) I could swap whatever value is received in the controller after submission. I may do one of these, but I’m not asking for possible solutions; I’m asking whether it is possible to make the HTML helper do as I wish. I have searched extensively and haven’t seen this addressed anywhere in official or unofficial sources. It seems like they should have a “swap” option or something.

  • 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-17T11:48:15+00:00Added an answer on June 17, 2026 at 11:48 am
        class ViewModel {
            public bool MyBoolean {get;set;}
            public bool DisplayValue {
                get {
                    return ! MyBoolean ;
                }
    
                set {
                    MyBoolean = !value;
                }
            }
        }
    

    And bind to the DisplayValue as it’s setter updates you MyBoolean property anyway.

    EDIT:

    After reading your question again:.

    You could use HtmlHelper to do that – but instead of using a CheckBox you could use a dropdown. The dropdown will define the “oppisite” values and text.

        myModelInstance.PossibleValues = new[] { new SelectListItem { Value = "false", Text = "Not false" }, new SelectListItem { Value = "true", Text = "Not true" } };
    

    Notice how the description is the opposite meaning of what you want the model to be. So for eg. for true you may have text as “Hidden” and false you may have the text as “Visible”, true for “Disabled” and false for “Enabled” etc.

    Then in your View:

        @Html.DropDownList(Model.MyBoolean.ToString(), Model.PossibleValues)
    

    The model will be updated with the correct value without have to do boolean toggles before viewing or updating.

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

Sidebar

Related Questions

I would like to run a str_replace or preg_replace which looks for certain words
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I am trying to render a haml file in a javascript response like so:
I have an autohotkey script which looks up a word in a bilingual dictionary
I have an array which has BIG numbers and small numbers in it. I
I have a text area in my form which accepts all possible characters from
I have an MVC Razor view @{ ViewBag.Title = Index; var c = (char)146;
I have thousands of HTML files to process using Groovy/Java and I need to
I would like my Web page http://www.gmarks.org/math_in_e-mail.txt on my Apache 2.2.14 server to display
I would like to count the length of a string with PHP. The string

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.