使用 EditorFor 传递附加视图模型数据在一对多关系中失败

发布于 2024-09-09 03:20:32 字数 895 浏览 5 评论 0原文

我有一个编辑器模板(Views/Shared/EditorTemplates/HORDER.ascx),它继承了

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

我的视图模型具有一对多关系,我使用 Html.EditorFor 来渲染多个 HORDERS

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

最终我想要做的是传入下拉列表的数据,但是,目前,我只是想传递一个字符串“hello”

如果我将以下内容嵌入到 HORDER.ascx 中,

<%: ViewData.Count() %>

我会得到“0”输出。任何获取视图数据的尝试都会失败。

但是,如果我将 EditorFor 与单个项目一起使用,它会按预期工作:我将 FirstHorder 添加到我的视图模型中,设置为 model.PEOPLE.HORDERS.First() 然后 <%: Html.EditorFor(model => model.FirstOrder,新的{fred =“你好”})%>按预期通过 hello。

那么...在使用一对多关系调用 EditorFor 时,有什么方法可以让 EditorFor 处理附加数据吗?

编辑: 其他地方建议我在 ForEach 循环中使用 EditorFor 来对抗各个 HORDERS。这是可行的(即附加的视图数据正确传递),但我最终得到由 EditorFor 呈现的每个控件都具有相同的 id 和名称,而不是 PEOPLE.HORDERS[n].field ,这是不可取的。

I have an editor template (Views/Shared/EditorTemplates/HORDER.ascx) which inherits

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(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"

If I embed the following in HORDER.ascx

<%: ViewData.Count() %>

I get "0" output. An any attempt to get hold of the viewdata fails.

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.

So... is there any way of getting EditorFor to work with additional data when calling it with a one-to-many relationship?

EDIT:
Its been suggested elsewhere that I use EditorFor in a ForEach loop against the individual HORDERS. This works (i.e. 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

凉月流沐 2024-09-16 03:20:32

经过一番混乱之后,似乎不可能做到这一点:我认为这是框架中的一个错误。解决方法是在调用 EditorFor 之前将其插入视图数据中。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文