Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
623 views
in Technique[技术] by (71.8m points)

razor - ASP.NET MVC - Can't bind array to view model

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" />
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
<input type="checkbox" id="[@i]" name="ChosenTestCollectionIds" value="@testCollection.Id" />

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.8k users

...