2

I have a simple form in my MVC3 site that allows users to create a contest entry. This has been implemented and works fine currently, but a request has been made to now allow users to make their entries private.

In my Entry model I added a boolean isPrivate. Then I figured I would change the HTML forms for create and edit to include a checkbox to specify whether the entry should be private.

I'm new to MVC3, but I figured I could simply change the action that the form posts to by including a new boolean parameter.

This unfortunately doesn't seem to work. Can anyone tell me how checkbox values are passed from an HTML form to a post action? This is probably fairly common, but I can't seem to find an example for this on the web. Almost all the examples out there simple show text inputs, I can't find anything with checkboxes.

Form:

        <form method="post" action="../Entry/Create" enctype="multipart/form-data" onsubmit="return isValidInput()">
            <input type="text" id="EntryTitle" name="EntryTitle" />
            <div id="invalidTitle" class="invalidData"></div>
            <p id="char-remaining">(100 characters remaining)</p>

            <input type="text" id="EntryVideo" name="EntryVideo" />
            <div id="invalidVideo" class="invalidData"></div>
            <p id="vid-desc">(URL of the Video to Embed)</p>

            <input type="file" id="ImageFile" name="ImageFile" />
            <div id="invalidImage" class="invalidData"></div>
            <p id="file-desc">(200x200px, jpeg, png, or gif)</p>

            <textarea id="EntryDesc" name="EntryDesc"></textarea>
            <div id="invalidDesc" class="invalidData"></div>
            <br />

            <input type="checkbox" id="isPrivate" name="isPrivate" />
            Make my entry private.

            <br />

            (private entries will only be viewable by you and site administrators)

            <br />


            <button id="new-entry-save">save</button>
        </form>

Action:

public ActionResult Create(string EntryTitle, string EntryVideo, HttpPostedFileBase ImageFile, string EntryDesc, Boolean isPrivate)
{
...
}
Danny
  • 3,615
  • 6
  • 43
  • 58

2 Answers2

3

add value="true" to checkbox, also add hidden input after it with same name and value=false, i.e.:

<input type="checkbox" id="isPrivate" name="isPrivate" value="true" />
<input type="hidden" name="isPrivate" value="false" />

If you don't want to use hidden, use bool? instead of bool (e.g. nullable)

Artem
  • 3,700
  • 1
  • 27
  • 35
  • 1
    When left unchecked, a server error is displayed when the form is posted, even with value="true". Should I be using a boolean for my action parameter isPrivate, or something else? – Danny Apr 12 '12 at 21:50
  • I edited my answer, sorry, forgot about required hidden field in your case – Artem Apr 12 '12 at 21:53
  • Cool this works now. So when the form posts it just ignores the second isPrivate input if the first is checked? I would have expected it to pass the last isPrivate value. – Danny Apr 12 '12 at 22:18
  • Yes, asp.net default binding work in this way, it will use first one (and this will be also valid for get request as well). Need to say that I usually wrap incoming parameters into supplementary class (e.g. viewmodel), e.g. `public ActionResult Create(CreateViewModel vm)`, where CreateViewModel contains postback parameters - this way you will also not need using additional hidden on client. – Artem Apr 12 '12 at 22:30
2

The other option is to have hidden text field with the same name to force data in unchecked field to be part of the post. See Post the checkboxes that are unchecked.

<form> 
  <input type='hidden' value='0' name='selfdestruct'> 
  <input type='checkbox' value='1' name='selfdestruct'> 
</form> 
Community
  • 1
  • 1
Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179