vendredi 23 octobre 2015

Checkbox state always passed to controller as null

Similar questions have been asked a couple times, but I can't seem to find an answer.

I am POSTing some values to a controller action and all the parameters are being passed successfully based off their names, besides the bool value of my checkbox. It is always passed as null.

Form:

@using (Ajax.BeginForm("AddNote", "Home", FormMethod.Post, new AjaxOptions { OnSuccess = "function noteAdded();", OnFailure = "alert(xhr.responseText)" }))
{
    <!-- New Note Modal -->
    <div class="modal fade" id="note-add-modal" tabindex="-1" role="dialog" aria-labelledby="note-add-modal">
        <div class="modal-dialog" role="document">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                    <h4 class="modal-title" id="note-add-modal-Label">Add A Note</h4>
                </div>

              <div class="modal-body">
                  <textarea placeholder="Description..." class="form-control" rows="5" id="comment" name="comment"></textarea>
                  <textarea name="assignTo" id="assign-to" rows="1" class="form-control" placeholder="Assign To..." data-autocomplete-url="@Url.Action("AutoCompleteUsername")"></textarea>
                  <br/>
                  <label id="follow-up-date">Follow-Up Date: <input name="alertDate" class="form-control-date" placeholder="dd/mm/yyyy" id="datepicker" type="text" /></label>
                  <label class="complete-label">Complete:</label>
                  <input type="checkbox" id="complete" name="complete" checked="checked"/>
                  <label for="complete"></label>
              </div>
              <div class="modal-footer">
                  <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
                  <button type="submit" id="note-form-submit" class="btn btn-primary">Save changes</button>
              </div>

            </div>
        </div>
    </div>

}

The checkbox has the name: complete.

Controller Action:

[HttpPost]
public void AddNote(string comment, DateTime? alertDate, bool complete, string assignedTo)
    {
        // Unrelated Code
    }

I thought you were able to pass the checkbox state to a controller as a bool value through the name. Or it is absolutely necessary to use @Html.Checkbox?

Thanks for your help!




Aucun commentaire:

Enregistrer un commentaire