简体   繁体   English

拆分MVC EditorFor以获取实体列表

[英]Splitting up MVC EditorFor for a list of entities

Hi I'm trying to make an app where you can create forms for event registrations and such. 嗨,我正在尝试制作一个应用程序,您可以在其中创建事件注册等的表单。 I have a Form model that has an ICollection of Fields. 我有一个具有ICollection of Fields的Form模型。 Fields is abstract and derives things like Checkbox or TextInput and such. 字段是抽象的,派生诸如Checkbox或TextInput之类的东西。 I've got it so the polymorphic behavior is working for my list of fields and I can get EditorFor templates for each kind of field so that I can do EditorFor on the whole list and it will generate the list with the correct template. 我已经了解到,所以多态行为适用于我的字段列表,并且我可以获取每种字段的EditorFor模板,以便可以在整个列表上使用EditorFor,它将使用正确的模板生成列表。

This is great and very easy with the entity framework, but my problem is that I want to be able to do some rendering logic between each field that is rendered on the screen. 使用实体框架,这非常好并且非常容易,但是我的问题是我希望能够在屏幕上呈现的每个字段之间执行一些呈现逻辑。 The fields are supposed to be rendered to a row/column grid and each Field has as properties Row / Size. 应该将这些字段呈现为行/列网格,并且每个Field都具有属性Row / Size。 The only way I've figured out how to use the EditorFor templates on the list of fields is like this 我弄清楚如何使用字段列表上的EditorFor模板的唯一方法是这样的

<div class="row">
    @Html.EditorFor(form => form.Fields)
</div>

But I need to be able to have some kind of loop instead where I have control of where that template is getting rendered.. For instance they are sorted by row so I can check to see if we're still adding fields to the same row or if I need to create a new row. 但是我需要能够有某种循环,而不是在我可以控制该模板的呈现位置的地方。例如,它们按行排序,因此我可以检查是否仍在同一行中添加字段或者如果我需要创建一个新行。

If I had to solve this by tomorrow I'd just render it as static html divs and then move it around using jQuery but I feel like that's a terrible way. 如果我明天必须解决这个问题,我只是将其呈现为静态html div,然后使用jQuery移动它,但我觉得那是一种糟糕的方法。 Another way would be to store the current row we're in in a Viewbag but then for each Field's template (like 12) I'd be just copying the same logic code... 另一种方法是将当前行存储在Viewbag中,但是对于每个Field的模板(如12),我将只复制相同的逻辑代码...

Any better ideas would be appreciated. 任何更好的想法将不胜感激。

EditorFor uses Tampletes that are store in Views/Shared/EditorTemplates. EditorFor使用存储在Views / Shared / EditorTemplates中的Tampletes。 You can there define your own tamplates for your type or override existing. 您可以在那里为您的类型定义自己的模板或覆盖现有模板。 Check this repo: https://github.com/danmalcolm/mvc-razor-display-and-editor-templates/tree/master/src/DemoMvcApp/Views/Shared/EditorTemplates 检查此仓库: https : //github.com/danmalcolm/mvc-razor-display-and-editor-templates/tree/master/src/DemoMvcApp/Views/Shared/EditorTemplates

I think in your case, you want to override Object.cshtml Like there: https://github.com/danmalcolm/mvc-razor-display-and-editor-templates/blob/master/src/DemoMvcApp/Views/Shared/EditorTemplates/Object.cshtml Am I right? 我认为在您的情况下,您想要覆盖Object.cshtml像这样: https : //github.com/danmalcolm/mvc-razor-display-and-editor-templates/blob/master/src/DemoMvcApp/Views/Shared/ EditorTemplates / Object.cshtml是吗?

My temporary solution was just to loop on the rows in my main view. 我的临时解决方案是在主视图中循环浏览行。 I store the current row index in ViewBag and then in the Editor templates I check if Model.Row == ViewBag.CurrRow. 我将当前行索引存储在ViewBag中,然后在编辑器模板中检查Model.Row == ViewBag.CurrRow。 I don't really like this because I'd be looping through all of the templates for the fields for every row I have. 我不太喜欢这样做,因为我将遍历我所拥有的每一行的所有模板。

I think the permanent solution will be a custom ViewModel that has the Fields split up into Rows so I can have an Editor template for each row. 我认为永久解决方案将是一个自定义ViewModel,该模型将Fields拆分为Rows,因此我可以为每一行提供一个Editor模板。

I also just found out that the EditorFor call on Model.Fields isn't actually calling EditorFor on all of the fields, it's calling it on the ICollection type, which in turn calls of the Field Editor templates. 我还发现,对Model.Fields的EditorFor调用实际上并不是在所有字段上都调用EditorFor,而是在ICollection类型上调用它,而ICollection类型又调用了Field Editor模板。 So I am pretty sure I can just make my own ICollection EditorTemplate that has the Row logic I need. 因此,我非常确定我可以仅制作自己的ICollection EditorTemplate,该模板具有所需的行逻辑。

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

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