通过模型的IEnumerable属性控制器后行动 - ASP MVC [英] Passing IEnumerable property of model to controller post action- ASP MVC

查看:134
本文介绍了通过模型的IEnumerable属性控制器后行动 - ASP MVC的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我工作的一个asp-MVC应用程序和所面临的以下问题:
这是一个工作code:

I'm working on an asp-MVC application and facing the following issue: Here is a working code:

<div id="detailsTemplates">
            @for (var i = 0; i < Model.Filter.itemDetails.Count; i++)
            {
                <table>
                    <tr>
                        <td>
                            @Html.TextBoxFor(model => model.Filter.itemDetails[i].iD)
                        </td>

                    </tr>
                </table>
            }

现在在我的控制器后我的行动得到正确插入的值。

Now in the post action in my controller I get the inserted value correctly.

现在,当我改变了code到局部视图:

Now, when I change the code to a partial view:

<div id="detailsTemplates">
            @for (int i = 0; i < Model.Filter.itemDetails.Count; i++)
            {
                Html.RenderPartial("itemDetailsUserControl", Model.Filter.itemDetails[i]);
            }



        </div>

和局部视图看起来如下:

and the partial view looks like the following:

<div id="detailsTemplates">
<table>
    <tr>
        <td>
            @Html.TextBoxFor(item => item.iD)
        </td>

    </tr>
</table>
}        

现在的价值不是通过后transffered。

Now the value is not transffered via the post.

我在做什么错了?

推荐答案

在视图中的模型的背景是被传递给它的模式。
例如。通过传递

The context of the model in the view is from the model being passed to it. E.g. by passing

Model.Filter.CustomsitemDetails[i]

你的部分,你已经从Model对象改变了根的CustomiseDetails对象。 (所以输入的名称将是CustomsItemClassificationID而不是Fi​​lter.CustomsitemDetails [I] .CustomsItemClassificationID等发布时,不会反弹,这将不再匹配它被绑定到的属性。

to your partial, you have changed the root from the Model object to your CustomiseDetails object. (so the name of the input will be "CustomsItemClassificationID" instead of "Filter.CustomsitemDetails[i].CustomsItemClassificationID", and so will not be rebound when posted, as will no longer match the property it is being bound to.

要做到这一点,正确的方法是使用的编辑模板即可。这将preserve模型的起源。要做到这一点,创建一个文件夹名为EditorTemplates下查看>共享。

The correct way to do this is to use an Editor Template. This will preserve the model's origin. To do this, Create a folder called EditorTemplates under Views > Shared.

您部分复制到它,并将其重命名称为你的类型的名称。例如,如果Model.Filter.CustomsitemDetails类型[i]为CustomsitemDetail则编辑模板文件夹下的文件将被称为CustomsitemDetails.cshtml。

Copy your partial into it, and rename it to be called the name of your type. For example, if the type of Model.Filter.CustomsitemDetails[i] is CustomsitemDetail then your file under the editor template folder will be called CustomsitemDetails.cshtml.

现在,而不是

@Html.RenderPartial("CustomsItemDetailsUserControl", Model.Filter.CustomsitemDetails[i]);

使用

@Html.EditorFor(m => m.Filter.CustomitemDetails[i])

另外,如果你不想重命名或移动文件时,可以指定它的当前位置,而不是:

@ Html.EditorFor(M => m.Filter.CustomitemDetails [I],〜/查看/ {}控制器/CustomsItemDetailsUserControl.cshtml)

更新

所以,我学到新的东西:

So I've learnt something new:

如果模板的名称与TEMPLATENAME参数相匹配的控制器的EditorTemplates文件夹中找到,则使用该模板呈现前pression。如果在控制器的EditorTemplates文件夹中找不到模板,视图\\共享\\文件夹EditorTemplates中搜索的TEMPLATENAME参数的名称相匹配的模板。如果没有找到模板,则使用默认模板。的距离的 MSDN

所以,违背我的回答的第二部分,你需要将你的部分,以

So contrary to the second part of my answer, you do need to move your partial to

"~/Areas/TaxCalculation/Views/Home/EditorTemplates/CustomsItemDetailsUserControl.cshtml"

和使用:

@Html.EditorFor(m => m.Filter.CustomitemDetails[i], "CustomsItemDetailsUserControl")

更新2

根据斯蒂芬的评论 - EditorFor()有一个接受IEnumerable的,所以你可以只使用过载

As per Stephen's comment - EditorFor() has an overload that accepts IEnumerable so you can just use

@Html.EditorFor(m => m.Filter.CustomitemDetails, "CustomsItemDetailsUserControl")

或者如果你有一个名为CustomitemDetails.cshtml的EditorTemplate位于/查看/共享/ EditorTemplates /文件夹:

Or if you have an EditorTemplate named CustomitemDetails.cshtml located in the /Views/Shared/EditorTemplates/ folder:

@Html.EditorFor(m => m.Filter.CustomitemDetails)

这篇关于通过模型的IEnumerable属性控制器后行动 - ASP MVC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆