Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have a view model with a from that includes a set of checkboxes. I need the check boxes to map to an array when binding in the post back method of my controller.

Here's the view model.

@model TMDM.Models.TestSeriesCreateViewModel

@{
    ViewBag.Title = "Create";
}

<h2>Create a Test Series</h2>


@using (Html.BeginForm()) {
    @Html.ValidationSummary(true)
    <fieldset>

        <div class="editor-label">
            @Html.LabelFor(model => model.Title)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Title)
            @Html.ValidationMessageFor(model => model.Title)
        </div>

        <h3>Which Test Collections are in this Test Series?</h3>
        <div class="editor-field">
        @{
            var i = 0;
            foreach (var testCollection in Model.TestCollections)
            {
                <input type="checkbox" id="ChosenTestCollectionIds[@i]" name="ChosenTestCollectionIds[@i]" value="@testCollection.Id" />
                <span>@testCollection.Title</span>
                <br />
                i++;
            }
         }
        </div>

        <p>
            <input type="submit" value="Save" class="medium green awesome" />
            @Html.ActionLink("Cancel", "Index", "TestSeries", null, new { @class = "medium black awesome" })
        </p>
    </fieldset>

The form is rendering fine, I've checked the source and each output check box has a different number for their id and name fields.

<input type="checkbox" id="ChosenTestCollectionIds[0]" name="ChosenTestCollectionIds[0]" value="5" />
<input type="checkbox" id="ChosenTestCollectionIds[1]" name="ChosenTestCollectionIds[1]" value="6" />
//etc...

Here is the view model.

public class TestSeriesModel
{
    public int Id { get; set; }
    public string Title { get; set; }
}

public class TestSeriesCreateViewModel : TestSeriesModel
{
    public List<ITestCollectionDataObject> TestCollections { get; set; }
    public int[] ChosenTestCollectionIds { get; set; }
}

Problem I'm having is that when the form posts back the ChosenTestCollectionIds array comes back null. What am I doing wrong here?

ANSWER

I've worked out how to do it:

<input type="checkbox" id="[@i]" name="ChosenTestCollectionIds" value="@testCollection.Id" />
share|improve this question
1  
Hello. I might help you more in a couple of hours, but in the meanwhile you want all of the checkboxes to have the same "name" attribute and different "id". Try that and see if it works. –  Hanlet Escaño Oct 3 '11 at 5:20
 
@payntbrush: Perfect. +1 for beating me to it by 2. –  Varun Vohra Oct 3 '11 at 5:47

3 Answers

up vote 3 down vote accepted
<input type="checkbox" id="[@i]" name="ChosenTestCollectionIds" value="@testCollection.Id" />
share|improve this answer

I always come back to Phil Haack's post about model binding a list. In addition, I always define my own index because my user's will alter the list on the client side then post back the changes.

http://haacked.com/archive/2008/10/23/model-binding-to-a-list.aspx

share|improve this answer

Set the name of the input types to all be the same. You can also create a custom model binder if you are trying to bind a more complex model than just a list. Here is an excellent article on the different ways to bind to your models

Various Model Binding techniques

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.