如何将来自多个对象的数据获取到我的视图?
所以我读过一些类似的问题,但我不知道是否提出了正确的问题。似乎有不同的方法可以从传递到视图模型的多个实体获取数据,但我想以正确的方式进行处理。
我的控制器中基本上有 2 个可用实体,并且我需要将两个实体中的不同信息传递到我的视图。我已阅读有关创建 SomeNameViewModel 类的信息,该类将在我的控制器 ViewResult 方法中实例化。使用 SomeNameViewModel 对象将数据分配到单个对象中以传递给视图模型
示例:
public ViewResult List()
{
var vm = new SomeNameViewModel {
products = _prodRepo.All();
catName = <Some Expression>;
return View(vm);
}
但这是解决此问题的最佳实践方法吗?
我正在使用 Nhibernate:那么在我的 Fluent 映射中可以更好地处理这个问题,以便我可以通过一对一映射访问其他实体吗?
So I have read some of the similar asked questions, but I don't know if the right questions were asked. It appears there are different ways to get data from multiple entities passed into your View Model, but I want to go about it the correct way.
I basically have 2 entities available in my controller, and I need to pass different information from both entities to my view. I have read about creating a SomeNameViewModel class that would be instantiated in my controller ViewResult method. With the SomeNameViewModel object assigning the data into a single object to pass to the View Model
Example:
public ViewResult List()
{
var vm = new SomeNameViewModel {
products = _prodRepo.All();
catName = <Some Expression>;
return View(vm);
}
But is this the best practice way to go about this?
I am using Nhibernate: So would this be better handled in my Fluent Mapping so that I have access to the other entity through the one-to-one mapping?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
每个视图使用一个模型是向视图提供数据的常见(也是好的)方法。视图模型可以包含来自多种实体类型的值,并且还可以包含辅助数据。您可能还需要考虑对视图模型中包含的任何实体使用特定于视图的模型,以进一步将视图与域对象隔离。通过这种方式,您可以准确地向视图提供它们需要的数据,而无需更多,如果您的域模型发生更改,您可能只能修改特定于视图的模型从域模型更新的方式,而不是在整个域模型中传播更改。意见。
Using a model per view is a common (and good) way to go about providing data to your views. View models can encompass values from more than one entity type and may contain ancillary data as well. You might want to also consider using view-specific models for any entities contained in your view model to further isolate your view from your domain objects. This way you can provide to your view exactly the data they need and no more and, if your domain model changes, you may be able to only modify how the view-specific model gets updated from the domain model rather than propagating the change throughout your views.