合并视图模型 [英] Merge ViewModel

查看:139
本文介绍了合并视图模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

即时通讯开始MVC和放大器;希望您对存储库和放意见;视图模型,具体几套库数据组合成一个虚拟机。

Im starting MVC & would like your advice about Repositories & View models, specifically combining a few sets of repository data into one VM.

如果我有需要自己的存储库(对不起,这个基本的性质)三个不同的部分(列)网页:

If I have a web page with three distinct sections (columns) that require their own repositories (sorry for the basic nature of this):

UserInfoData - 在左山坳所示

UserInfoData - shown in Left col

DiaryData - 在山坳中间所示

DiaryData - shown in Middle col

CommentData - 图中所示为右山坳

CommentData - Shown in Right col

Im做以下几点: -

Im doing the following :-

public ActionResult Index(int id)
{
  UserInfoData uid = repository.GetUserInfoData(id);
  UserInfoDataVM UserViewModel = Mapper.Map<UserInfoData, UserInfoDataVM>(uid);

  //RETRIEVE DiaryData & CommentData using same principle as above

  return ??
}

我将如何结合这三个虚拟机的到的东西我能回到看法 - 我用另一台虚拟机,它接受这三个虚拟机

How would I combine these three VM's into something I can return to the view - do I use another VM which accepts these three VM's?

是否有与具有自己的数据网页的小段打交道时,最好的做法?

Is there a best practice when dealing with smaller segments of a web page that have their own data?

推荐答案

在我看来,这取决于你的使用场景。

In my opinion, it depends on your usage scenario.

您可以创建一个包含三个子视图模型视图模型,但你需要问自己,我该怎么使用任何三个子视图模型其他地方呢?如果没有,那么你可以只使用一个大的视图模型。否则,没有什么不对的小封装视图模型的包装视图模式。

You could create a View Model that contains the three sub view models, but you would need to ask yourself, am I going to use any of the three sub view models anywhere else? If not, then you could just use one large view model. Else, there is nothing wrong with the 'wrapper' view model that encapsulates smaller view models.

您可以保持你在上面定义的三个视图模型,你可以使用Html.RenderAction语法在视图中其实去提取每个页面的子部件。你将有你的控制器,其职责是填充每个小视图模型和返回认为该机型定义的操作。

You could keep the three view models as you have defined above and you could use the Html.RenderAction syntax in your view to actually go and fetch each of the sub parts of your page. You would have defined an action on your controller whose responsibility it is to populate each of the small view models and return the view for that model.

<div id="leftCol">
   @{Html.RenderAction("LeftCol", "Controller");}
</div>
<div id="midCol">
   @{Html.RenderAction("MiddleCol", "Controller");}
</div>
<div id="rightCol">
   @{Html.RenderAction("RightCol", "Controller");}
</div>

这会给你使用的子部件上的网站的任何地方的能力,你只需要定义一次操作方法和局部视图。

This would give you the ability to use the sub parts anywhere on your site and you only have to define the action method and partial views once.

第二个选择也是个人最喜欢的,因为你可以迅速利用JQuery的做局部页面加载,如果这些部分需要几秒钟来填充和渲染(如果这是有道理的为您的网页)。现在,你的页面打开很快,你有一个JS函数去,并获取这些部分,把一个小图形负荷超过他们,并使用AJAX请求加载/渲染。同样,只有当这是有道理的你,但使用的局部视图/渲染操作方法通常给你整个网站和AJAX部分页面呈现重用更多的灵活性。

The second option is also a personal favorite because you can quickly leverage JQuery to do partial page loading if these sections take a few seconds to populate and render (if this makes sense for your page). Now, your page opens quickly, you have a JS function that goes and fetches these sections, put a little loading graphic over them and are loaded/rendered using an AJAX request. Again, only if this makes sense for you, but using the Partial Views/Render action methods typically give you more flexibility in reuse across the site and AJAX partial page rendering.

这篇关于合并视图模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆