到目前为止,我们可以使用 Html.EditorFor() 动态呈现数据类型的适当模板 - 例如字符串、整数或自定义类型,例如“地址”。
现在我想使用 EditorFor() 渲染“父”字段。我想要一个包含每一行的下拉列表,并且用户从该下拉列表中选择一个父级。
“Parent”模板可以访问“ParentID”,但是用于填充列表框的行的名称和 ID 又如何呢?
这些应该从哪里来?
我可以将数据访问逻辑放入模板中,但这会违反关注点分离。
我可以创建一个呈现列表的 HtmlHelper,但这是否也会破坏关注点分离,因为 HtmlHelpers 应该只执行 UI 操作,而不是数据访问?
有什么想法吗?
So far we can use Html.EditorFor() to dynamically render the appropriate template for a datatype - e.g. string, int, or a custom type, say 'Address'.
Now I want to use EditorFor() to render a 'Parent' field. I want a drop-down containing every row, and the user picks a parent from this drop-down.
The 'Parent' template has access to the 'ParentID', but what about the names and ID's of rows, to populate the list-box with?
Where should these come from?
I could put data-access logic in the template, but that would be violating the separation of concerns.
I could create an HtmlHelper that renders the list, but wouldn't this also break separation of concerns, since HtmlHelpers should only do UI stuff, not data access?
Any ideas?
发布评论
评论(1)
也许您可以尝试 MVVM 方法?您的 ViewModel 将包含呈现下拉列表所需的所有数据,以及用于存储用户在 POST 上输入的“编辑”模型,我猜在本例中这只是一个 ID 号。
You could try a MVVM approach, maybe? Your ViewModel would contain all the data you need to render the dropdown, as well as an "edit" model to store the user's input upon a POST, which would just be an ID number in this case, I guess.