ASP.NET MVC 3:处理列表列表的 ViewModel
我正在尝试组合一个 ViewModel,该 ViewModel 将具有用户列表,并且每个用户将具有位置列表。 用户表和位置表通过另一个表连接在一起,该表保存各自的 ID 和一些其他信息。该表本质上是一个多对多连接表。 我尝试了几种不同的 viewModel 方法,但我们严重缺乏它们......显示此类信息的最佳方法是什么?
I'm trying to put together a ViewModel that will have a list of users and each user will have a list of locations.
The User table and Location table are joined together through another table that holds each respective ID and some other information. This table is essentially a many to many join table.
I've tried a few different viewModel approaches and they we're severely lacking... What would be the best approach for displaying this type of information?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我假设问题是您希望能够通过用户或位置访问集合。一种方法是使用
ILookup
类。您将从多对多集合开始并生成如下查找:更新:
根据您的描述,似乎您实际上并不需要拥有完整的多对多ViewModel 中的关系。相反,您的 ViewModel 可以具有如下结构:
如果您想避免冗余
LocationViewModel
对象,您可以在 Model 和 ViewModel 对象之间预先构建映射:然后在填充页面的视图模型。
I assume that the issue is that you want to be able to access the collection by either User or Location. One approach could be to use
ILookup<>
classes. You'd start with the many-to-many collection and produce the lookups like this:Update:
Per your description, it seems like you don't really need to have a full many-to-many relationship in your ViewModel. Rather, your ViewModel could have a structure like this:
If you wanted to avoid redundant
LocationViewModel
objects, you could pre-build a mapping between your Model and ViewModel objects:And then reuse these objects when populating your page's ViewModel.