ASP.NET MVC - 无法绑定数组视图模型 [英] ASP.NET MVC - Can't bind array to view model
问题描述
我有一个从该视图模型包括一组复选框。我需要的复选框来映射到一个数组中后我控制器的背法结合的时候。
下面是视图模型。
@model TMDM.Models.TestSeriesCreateViewModel@ {
ViewBag.Title =创建;
}< H2>创建一个测试系列< / H>
@using(Html.BeginForm()){
@ Html.ValidationSummary(真)
<&字段集GT; < DIV CLASS =编辑标记>
@ Html.LabelFor(型号=> model.Title)
< / DIV>
< DIV CLASS =主编场>
@ Html.EditorFor(型号=> model.Title)
@ Html.ValidationMessageFor(型号=> model.Title)
< / DIV> < H3>哪些测试集合在这个测试系列< / H3 GT&;
< DIV CLASS =主编场>
@ {
变种I = 0;
的foreach(在Model.TestCollections VAR为TestCollection)
{
<输入类型=复选框ID =ChosenTestCollectionIds [@i]NAME =ChosenTestCollectionIds [@i]VALUE =@ testCollection.Id/>
<跨度> @ testCollection.Title< / SPAN>
< BR />
我++;
}
}
< / DIV> &所述p为H.;
<输入类型=提交值=保存级=中的绿色真棒/>
@ Html.ActionLink(取消,索引,TestSeries,空,新的{@class =中等黑真棒})
&所述; / P>
< /字段集>
的形式呈现细,我检查源和每个输出复选框有自己的ID和姓名字段不同的号码。
<输入类型=复选框ID =ChosenTestCollectionIds [0]NAME =ChosenTestCollectionIds [0]值=5/>
<输入类型=复选框ID =ChosenTestCollectionIds [1]NAME =ChosenTestCollectionIds [1]VALUE =6/>
//等等...
下面是视图模型。
公共类TestSeriesModel
{
公众诠释标识{搞定;组; }
公共字符串名称{搞定;组; }
}公共类TestSeriesCreateViewModel:TestSeriesModel
{
公开名单< ITestCollectionDataObject> TestCollections {搞定;组; }
公众诠释[] {ChosenTestCollectionIds获得;组; }
}
我遇到的问题是,当窗体回发的ChosenTestCollectionIds阵列回来空。我在做什么错在这里?
答案
我已经计算出如何做到这一点:
<输入类型=复选框ID =[@我]NAME =ChosenTestCollectionIdsVALUE =@ testCollection.Id/>
<输入类型=复选框ID =[@我]NAME =ChosenTestCollectionIds值= @ 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屋!