ASP.NET MVC - 无法将数组绑定到视图模型 [英] ASP.NET MVC - Can't bind array to view model

查看:17
本文介绍了ASP.NET MVC - 无法将数组绑定到视图模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含一组复选框的视图模型.在控制器的回发方法中绑定时,我需要将复选框映射到数组.

这是视图模型.

@model TMDM.Models.TestSeriesCreateViewModel@{ViewBag.Title = "创建";}<h2>创建测试系列</h2>@using (Html.BeginForm()) {@Html.ValidationSummary(true)<字段集><div class="editor-label">@Html.LabelFor(model => model.Title)

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

<h3>本测试系列中有哪些测试集?</h3><div class="editor-field">@{变量 i = 0;foreach(Model.TestCollections 中的 var testCollection){<input type="checkbox" id="ChosenTestCollectionIds[@i]" name="ChosenTestCollectionIds[@i]" value="@testCollection.Id"/><span>@testCollection.Title</span><br/>我++;}}

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

表单呈现良好,我检查了源代码,每个输出复选框的 id 和 name 字段都有不同的编号.

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

这是视图模型.

公共类TestSeriesModel{公共 int Id { 获取;放;}公共字符串标题{获取;放;}}公共类 TestSeriesCreateViewModel : TestSeriesModel{公共列表测试集合 { 获取;放;}public int[] ChosenTestCollectionIds { get;放;}}

我遇到的问题是,当表单回发时,ChosenTestCollectionIds 数组返回空值.我在这里做错了什么?

答案

我已经找到了方法:

解决方案

<input type="checkbox" id="[@i]" name="ChosenTestCollectionIds" value="@testCollection.Id"/>

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" />

解决方案

<input type="checkbox" id="[@i]" name="ChosenTestCollectionIds" value="@testCollection.Id" />

这篇关于ASP.NET MVC - 无法将数组绑定到视图模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
其他开发最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆