asp.net-mvc ASP.NET MVC - 无法将数组绑定到视图模型
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7631212/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
ASP.NET MVC - Can't bind array to view model
提问by Chris
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.
表单呈现良好,我检查了源代码,每个输出复选框的 id 和 name 字段都有不同的编号。
<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?
我遇到的问题是,当表单回发时,ChosenTestCollectionIds 数组返回空值。我在这里做错了什么?
ANSWER
回答
I've worked out how to do it:
我已经想出了如何做到这一点:
<input type="checkbox" id="[@i]" name="ChosenTestCollectionIds" value="@testCollection.Id" />
采纳答案by Chris
<input type="checkbox" id="[@i]" name="ChosenTestCollectionIds" value="@testCollection.Id" />
回答by Khalid Abuhakmeh
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.
我总是回到 Phil Haack 关于模型绑定列表的帖子。此外,我总是定义自己的索引,因为我的用户会在客户端更改列表,然后回发更改。
http://haacked.com/archive/2008/10/23/model-binding-to-a-list.aspx
http://haacked.com/archive/2008/10/23/model-binding-to-a-list.aspx
回答by Ryand.Johnson
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
将输入类型的名称设置为全部相同。如果您尝试绑定比列表更复杂的模型,您还可以创建自定义模型绑定器。这是一篇关于绑定到模型的不同方法的优秀文章

