简体   繁体   English

在一对多关系中,使用EditorFor传递其他viewmodel数据失败

[英]Passing additional viewmodel data with EditorFor fails in one-to-many relationships

I have an editor template (Views/Shared/EditorTemplates/HORDER.ascx) which inherits 我有一个继承的编辑器模板(Views / Shared / EditorTemplates / HORDER.ascx)

System.Web.Mvc.ViewUserControl<CCOK2.Models.HORDER>

My viewmodel has a one-to-many relationship which I use Html.EditorFor in order to render multiple HORDERS 我的视图模型具有一对多关系,我使用Html.EditorFor来渲染多个HORDERS

<%: Html.EditorFor(model => model.PEOPLE.HORDERS, new {fred="hello"})%>

Eventually what I want to do is pass in the data for a dropdownlist, however, for the moment, I'm just trying to pass in a string "hello" 最终,我想做的是传递一个下拉列表的数据,但是,目前,我只是想传递一个字符串“ hello”

If I embed the following in HORDER.ascx 如果我将以下内容嵌入HORDER.ascx

<%: ViewData.Count() %>

I get "0" output. 我得到“ 0”输出。 An any attempt to get hold of the viewdata fails. 任何试图获取viewdata的尝试都会失败。

However if I use EditorFor with a single item it works as expected : I added FirstHorder to my viewmodel, set is as model.PEOPLE.HORDERS.First() then <%: Html.EditorFor(model => model.FirstOrder, new {fred="hello"})%> passes hello as expected. 但是,如果我将EditorFor与单个项目一起使用,则可以按预期方式工作:我在视图模型中添加了FirstHorder,设置为model.PEOPLE.HORDERS.First(),然后<%:Html.EditorFor(model => model.FirstOrder,新{ fred =“ hello”})%>按预期传递问候。

So... is there any way of getting EditorFor to work with additional data when calling it with a one-to-many relationship? 那么...通过一对多关系调用EditorFor时,有什么方法可以处理其他数据?

EDIT: Its been suggested elsewhere that I use EditorFor in a ForEach loop against the individual HORDERS. 编辑:有人建议我在ForEach循环中针对单个HORDERS使用EditorFor。 This works (ie the additional viewdata is passed correctly) but I end up with every control rendered by the EditorFor having the same id and name rather than PEOPLE.HORDERS[n].field which is not desirable. 这是可行的(即正确地传递了附加的视图数据),但最终我得到了由EditorFor呈现的每个控件,它们具有相同的ID和名称,而不是PEOPLE.HORDERS [n] .field,这是不希望的。

After much messing it seems that it is not possible to do this: I consider that a bug in the framework. 经过一番混乱之后,似乎无法做到这一点:我认为这是框架中的错误。 The workaround is to insert it into the viewdata prior to the EditorFor call. 解决方法是在EditorFor调用之前将其插入到视图数据中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM