MVC 6视图组件与部分视图:有什么区别?什么时候应该使用? [英] MVC 6 View Components vs. Partial Views: What is the difference? When should each be used?

查看:125
本文介绍了MVC 6视图组件与部分视图:有什么区别?什么时候应该使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

MVC 6引入了View组件,并说它比部分视图要强大和灵活.视图组件是用来替换部分视图的吗?每种实现有什么区别和需要哪种情况?

MVC 6 Introduced View components and said it is much stronger and flexible than partial views. Are view components meant to replace partial views? What is the difference and what kinds of situations call for each implementation?

推荐答案

根据此链接- https://docs.asp.net/en/latest/mvc/views/view-components

ASP.NET MVC 6的新增功能,视图组件(VC)与部分视图相似,但是功能更强大. VC在控制器和视图之间具有相同的关注点分离和可测试性好处.您可以将VC视为微型控制器,它负责呈现块而不是整个响应.

New to ASP.NET MVC 6, view components (VCs) are similar to partial views, but they are much more powerful. VCs include the same separation-of-concerns and testability benefits found between a controller and view. You can think of a VC as a mini-controller—it’s responsible for rendering a chunk rather than a whole response.

因此,这只是对部分视图的增强,另一个区别是,当您使用部分视图时,您仍然依赖于控制器,而在视图组件"中则不需要控制器.因此,有一个关注点分离.

So it just a enhancement of partial view and another difference is when you use partial view you still have dependency on controller while in View Component you don't need a controller. So there is a separation of concern.

有一个有关ASP.NET View组件的详细信息. http://www.tugberkugurlu .com/archive/exciting-things-about-asp-net-vnext-series-mvc-view-components

There is a detail post for ASP.NET View Components. http://www.tugberkugurlu.com/archive/exciting-things-about-asp-net-vnext-series-mvc-view-components

这篇关于MVC 6视图组件与部分视图:有什么区别?什么时候应该使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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