ASP.NET MVC2 - 模型在一个视图页面上绑定多个数据源 - 选项?
我有一个视图页面 (MyView.aspx) 和许多要在此页面上绑定的数据源。
假设它有书籍、出版商、评论等。
其中每一个都有一个提供列表、列表等的对象。
在我看来,多模型竞价的选择是什么?
我想检查每个是否为空,然后枚举它。但我无法检查 Model.Count() ,因为如果我将页面设置为继承,模型不会由所有这些对象组成吗?
我有什么选择?我应该在控件/部分视图中加载每个内容区域吗?
或者我可以将每个对象转储到 ViewData 中,然后通过在视图中进行转换来检查计数吗?
非常感谢您查看我的问题。
I have one view page (MyView.aspx) and many data sources to bind on this page.
Lets say it has Books, Publishers, Comments, etc.
Each one of those has an object which provides a List, List, etc.
In my view, what are my optiosn for multilple model biding?
I want to check to see if each one is empty, and then enumerate it. But I can't check Model.Count() because wouldn't Model be made of all those objects if I set the page to inheriet from ?
What are my options? Should I load each content area in a control/partial view?
Or can I just dump each object into ViewData and then check the count by casting in the view?
Thanks so much for taking a look at my problem.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您是否考虑过使用包含所有不同数据字段的所有列表的 ViewModel 并使用它来填充您的视图?
示例:
ViewModel:
然后在您的视图中,您可以简单地在枚举之前检查特定字段是否为空:
View:
Have you considered using a ViewModel that contains all Lists of all of your different data fields and using that to populate your View?
Example:
ViewModel:
Then in your View you could simply check if a specific field was null prior to enumerating through it:
View:
Rionmonster 的可能是最好的解决方案 - 另一个是使用强类型部分视图。您可以将所有内容加载到视图数据中,然后将每个段注入到相应的部分视图中。
Rionmonster's is probably the best solution - another is to use strongly typed partial views. You could load everything into the view data and then inject each segment into the respective partial view.