locked
Hidden Bool value RRS feed

  • Question

  • User1463147114 posted

    I do not understand why this sample code is not returning the bool value "true".

    I am using Webmatrix version 3.

    @{   
        bool archive = false;
        
        if(!IsPost)
          {
              archive = true;
             <p>Is Not Post:  @archive  </p>
          } 
     
        if(IsPost)
         {  <p>Post:  @archive  </p>
            <p>Request: @Request["archive"].AsBool()</p>    
         }  
    }
     
    <!DOCTYPE html>
    <html lang="en">
    <body>  
    <form method="post">
        <p>Hidden: @archive</p>
         <input type="hidden" name="archive" value="@archive" />               	       	
      
         <input type ="submit" name="buttonSubmit"/> 
     
    </form>
    </body>
    </html>
    Wednesday, July 29, 2015 7:55 PM

Answers

  • User-821857111 posted

    It's because of a Web Pages feature called conditional attributes (http://www.mikesdotnetting.com/article/201/cleaner-conditional-html-attributes-in-razor-web-pages). If you set the value attribute of an HTML element to True, the attribute name is passed as the value to the attribute when it is rendered, so checked="true" becomes checked="checked" and value="true" becomes value="value". If you try to convert the string "value" to a boolean, you will get false. So you should avoid passing "true" or "false" as values to HTML attributes in Web Pages. You can use integers instead:

    @{   
        bool archive = false;
        
        if(!IsPost)
          {
              archive = true;
             <p>Is Not Post:  @archive  </p>
          } 
     
        if(IsPost)
         {  <p>Post:  @archive  </p>
            <p>Request: @Convert.ToBoolean(Request["archive"].AsInt())</p>    
         }  
    }
     
    <!DOCTYPE html>
    <html lang="en">
    <body>  
    <form method="post">
        <p>Hidden: @archive</p>
         <input type="hidden" name="archive" value="@Convert.ToInt32(archive)" />               	       	
      
         <input type ="submit" name="buttonSubmit"/> 
     
    </form>
    </body>
    </html>

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, July 30, 2015 2:36 AM

All replies

  • User-821857111 posted

    It's because of a Web Pages feature called conditional attributes (http://www.mikesdotnetting.com/article/201/cleaner-conditional-html-attributes-in-razor-web-pages). If you set the value attribute of an HTML element to True, the attribute name is passed as the value to the attribute when it is rendered, so checked="true" becomes checked="checked" and value="true" becomes value="value". If you try to convert the string "value" to a boolean, you will get false. So you should avoid passing "true" or "false" as values to HTML attributes in Web Pages. You can use integers instead:

    @{   
        bool archive = false;
        
        if(!IsPost)
          {
              archive = true;
             <p>Is Not Post:  @archive  </p>
          } 
     
        if(IsPost)
         {  <p>Post:  @archive  </p>
            <p>Request: @Convert.ToBoolean(Request["archive"].AsInt())</p>    
         }  
    }
     
    <!DOCTYPE html>
    <html lang="en">
    <body>  
    <form method="post">
        <p>Hidden: @archive</p>
         <input type="hidden" name="archive" value="@Convert.ToInt32(archive)" />               	       	
      
         <input type ="submit" name="buttonSubmit"/> 
     
    </form>
    </body>
    </html>

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, July 30, 2015 2:36 AM
  • User325035487 posted

    Wow thanks.

    Thursday, July 30, 2015 10:50 AM