locked
Spaces in a string RRS feed

  • Question

  • User1463147114 posted

    Hello.

    Below is a simple example to illustrate the problem I'm having when a string is used in a hidden field.

    The source code shows the value ends with the space in "Vice President". Why, and how do I get around this issue?

    Thanks.

    @{
         string position = "Director,President,Vice President,Secretary,Treasurer";
     
         if(IsPost)
                {
                    position = Request["position"];
                    <p>Position: @position</p>
                }
    }
     
    <!DOCTYPE html>
    <html lang="en">
        <body>
            <form method="post">
            <p>@position</p>
            <input type="hidden" name="position" value= @position />
            <p><input class="green" type="submit" name="buttonSubmit" value="Submit" /></p>
            </form>
        </body>
    </html>

    Thursday, October 9, 2014 1:52 PM

Answers

  • User-821857111 posted

    You need some quotes:

    <input type="hidden" name="position" value="@position"  />

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, October 9, 2014 2:16 PM

All replies

  • User-821857111 posted

    You need some quotes:

    <input type="hidden" name="position" value="@position"  />

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, October 9, 2014 2:16 PM
  • User1463147114 posted

    Ah yes, makes sense for strings. Those quotes aren't necessary for numeric values and that is what I copied from.

    Lesson learned.

    Thx Mike.

    Thursday, October 9, 2014 3:18 PM
  • User-821857111 posted

    Those quotes aren't necessary for numeric values
    A rare backward step introduced by HTML5. However, I always quote attribute values.

    Thursday, October 9, 2014 4:37 PM