在ViewModel中绑定IEnumerable不工作。

时间:2021-12-23 16:55:26

I have a ViewModel - Customer. Within this there is an IEnumerable collection:

我有一个视图模型——客户。其中有一个IEnumerable集合:

public class Tag
{
    public int TagNo{ get; set; }
    public DateTime CreatedDate { get; set; }
}

I output a list of tags to the page - along with the scalar customer information - using a foreach:

我在页面上输出了一个标签列表——以及标量客户信息——使用foreach:

...output customer info....
<div class="col-md-9">
    @{
        foreach (var Tag in Model.Tags)
        {
            @Html.DisplayFor(x => Tag.TagNo);
            <br />
        }
    }
</div>

I am attempting to hide the tag list in the page for the model binder so on errors I don't need to hit the database again:

我正试图将标记列表隐藏在模型绑定器的页面中,因此对于错误,我不需要再次访问数据库:

@{
    int[] tagArray = Model.Tags.Select(x => x.TagNo).ToArray();

    for (int i = 0; i < Model.Tags.Count(); i++)
    {
        @Html.Hidden("Tags[" + i + "]", tagArray[i])
    }
}

This outputs this:

这个输出:

<input id="Tags_0_" name="Tags[0]" type="hidden" value="21111111" />
etc

I have seen various questions on SO saying you can use an array (as above) in a certain format that the model binder will understand for binding lists - eg:

我已经看到了各种各样的问题,所以说您可以使用一个特定格式的数组(如上所述),模型绑定器将理解绑定列表。

Creating HiddenFor IEnumerable<String> in View

在视图中创建HiddenFor IEnumerable

However, when the page has an error and I use the debugger to see what has been bound on the post, Although model.tags appears to have 3 elements - they are all empty.

但是,当页面出现错误时,我使用调试器查看post上绑定的内容,尽管是模型。标签似乎有3个元素——它们都是空的。

I have also tried this:

我也尝试过:

@Html.Hidden("Tags[" + i + "].TagNo", tagArray[i])

But the Customer.Tags collection is empty after posting.

但客户。标签收集在张贴后是空的。

What am I doing wrong? How can I hide this list of tags in the page so that the model binder will see it?

我做错了什么?如何在页面中隐藏标签列表,以便模型绑定器看到它?

1 个解决方案

#1


1  

Your model property is IEnumerable<Tag> but following line(s) posting int[].

您的模型属性是IEnumerable ,但是遵循line(s)发布int[]。

you need change

你需要改变

@{
    int[] tagArray = Model.Tags.Select(x => x.TagNo).ToArray();

    for (int i = 0; i < Model.Tags.Count(); i++)
    {
        @Html.Hidden("Tags[" + i + "]", tagArray[i])
    }
}

to

@for (int i = 0; i < @Model.Tags.Count; i++)
{
   <input id="Tags_@(i)__TagNo" name="Tags[@i].TagNo" type="hidden" value="@Model.Tags[i].TagNo" />
}

I recommend that using editor templates for such situations

我建议在这种情况下使用编辑器模板

\Views\Shared\EditorTemplates\Tag.cshtml :

\ Views \ \ EditorTemplates \共享标签。cshtml:

@model mvcTest.Models.Tag
@Html.HiddenFor(a => a.TagNo)

parent view :

父视图:

@for (int i = 0; i < @Model.Tags.Count; i++)
{
    @Html.EditorFor(a => a.Tags[i])
}

#1


1  

Your model property is IEnumerable<Tag> but following line(s) posting int[].

您的模型属性是IEnumerable ,但是遵循line(s)发布int[]。

you need change

你需要改变

@{
    int[] tagArray = Model.Tags.Select(x => x.TagNo).ToArray();

    for (int i = 0; i < Model.Tags.Count(); i++)
    {
        @Html.Hidden("Tags[" + i + "]", tagArray[i])
    }
}

to

@for (int i = 0; i < @Model.Tags.Count; i++)
{
   <input id="Tags_@(i)__TagNo" name="Tags[@i].TagNo" type="hidden" value="@Model.Tags[i].TagNo" />
}

I recommend that using editor templates for such situations

我建议在这种情况下使用编辑器模板

\Views\Shared\EditorTemplates\Tag.cshtml :

\ Views \ \ EditorTemplates \共享标签。cshtml:

@model mvcTest.Models.Tag
@Html.HiddenFor(a => a.TagNo)

parent view :

父视图:

@for (int i = 0; i < @Model.Tags.Count; i++)
{
    @Html.EditorFor(a => a.Tags[i])
}