使用 MVC 3 编辑集合中的集合
我正在尝试为以下场景创建编辑视图:
Profile class { String profileName, IList<Phase> phases; //plus there getter setter}
Phase class { String phaseName, IList<SubPhase> subPhases; //plus there getter setter}
SubPhase class { String subPhaseName // plus there getter setter}
我已经使用 @model CollectionEditing.Models.Profile
创建了配置文件视图,并创建了两个部分视图作为 PhaseEditor 和 SubPhaseEditor
我的配置文件视图我正在使用部分视图:
//iteration the phases
for (int i = 0; i < Model.Phases.Count; i++)
{
//rendering the partial view for Phases
Html.RenderPartial("PhaseEditor", Model.Phases[i]);
//iteration the SubPhases
for (int j = 0; j < Model.Phases[i].SubPhases.Count; j++)
{
//rendering the partial view for SubPhases
Html.RenderPartial("SubPhaseEditor", Model.Phases[i].SubPhases[j]);
}
}
可编辑视图已成功创建。
当我单击“提交”按钮时,在我的操作中,我获取了配置文件及其阶段列表的值,但没有获取阶段类中的子阶段列表。子阶段列表设置为空。
我还尝试将 Html.RenderPartial("SubPhaseEditor", Model.Phases[i].SubPhases[j]);
放在部分视图 PhaseEditor 中,但仍然不起作用。
请帮助...我怎样才能获得子阶段列表。
非常感谢您的帮助。
苏希尔
I am trying to create a Edit View for the following scenario:
Profile class { String profileName, IList<Phase> phases; //plus there getter setter}
Phase class { String phaseName, IList<SubPhase> subPhases; //plus there getter setter}
SubPhase class { String subPhaseName // plus there getter setter}
I have created Profile View with @model CollectionEditing.Models.Profile
and two partial view as PhaseEditor and SubPhaseEditor
I profile view I am using partial view:
//iteration the phases
for (int i = 0; i < Model.Phases.Count; i++)
{
//rendering the partial view for Phases
Html.RenderPartial("PhaseEditor", Model.Phases[i]);
//iteration the SubPhases
for (int j = 0; j < Model.Phases[i].SubPhases.Count; j++)
{
//rendering the partial view for SubPhases
Html.RenderPartial("SubPhaseEditor", Model.Phases[i].SubPhases[j]);
}
}
The Editable View is created successfully.
When I click the Submit button In my action I getting the values for Profile and its list of Phases and I am not getting the list of Subphase in Phase class. list of subphases is set to null.
I have also tried putting Html.RenderPartial("SubPhaseEditor", Model.Phases[i].SubPhases[j]);
in partial view PhaseEditor but still it does not work.
Please help... How I can get the list of SubPhases also.
Thank you very much for your help.
Sushil
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的意思是 ModelBinder 没有将“完整”对象传递给您的控制器?
这不会自动为您完成。 ModelBinder 不使用底层集合。您必须自己创建这个集合。在控制器中使用 FormCollection 作为参数并迭代它以创建集合。
这篇文章展示了如何通过 FormCollection 进行枚举:
如何在 ASP.NET MVC 中枚举表单集合?
You mean the ModelBinder is not passing a 'full' object to your controller?
That will not be done automatically for you. Underlying collections are not used by the ModelBinder. You will have to create this collection by yourself. Use FormCollection in your controller as a parameter and iterate through it to create the collection.
This post is showing how to enumerate through the FormCollection:
How can a formcollection be enumerated in ASP.NET MVC?