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)
{
...
}
2
  • What happens if you make isPrivate a string in your action signature? Commented Apr 12, 2012 at 21:41
  • Why don't you use the HTML-helper: <%=Html.Checkbox("isPrivate") %>? It automatically creates a hidden field, in case you don't check the box. Commented Apr 12, 2012 at 21:53

2 Answers 2

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)

Sign up to request clarification or add additional context in comments.

4 Comments

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?
I edited my answer, sorry, forgot about required hidden field in your case
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.
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.
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> 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.