本文转自:http://www.c-sharpcorner.com/uploadfile/8c19e8/asp-net-5-getting-started-with-asp-net-mvc-6/

Introducing View Components Another cool feature in MVC 6 is the “View Components”. If you remember, in previous versions of ASP.NET MVC, the Html.Action() helper is typically used to invoke a sub-controller. A sub-controller may display stuff like tag clouds, dynamic links, side bars or whatever. ASP.NET MVC 6 introduced the new View Component to replace widgets that use Html.Action().
View Components also supports fully async allowing you to make a view component asynchronous.
Now let's try to create a very simple view component and let's see how they are used in MVC. To start, create a new folder at the root of your application and name it “ViewComponents”. Within that folder create a new class and name it “HeroListViewComponent” and copy the following code:

  1. using Microsoft.AspNet.Mvc;
  2. using System.Threading.Tasks;
  3. using MVC6Demo.Models;
  4. using System.Collections.Generic;
  5. namespace MVC6Demo.ViewComponents
  6. {
  7. public class HeroListViewComponent: ViewComponent
  8. {
  9. public async Task < IViewComponentResult > InvokeAsync(string type)
  10. {
  11. var heroes = await GetHeroesAsync(type);
  12. return View(heroes);
  13. }
  14. private Task < IEnumerable < DOTAHero >> GetHeroesAsync(string type)
  15. {
  16. return Task.FromResult(GetHeroes(type));
  17. }
  18. private IEnumerable < DOTAHero > GetHeroes(string type)
  19. {
  20. HeroManager HM = new HeroManager();
  21. return HM.GetHeroesByType(type);
  22. }
  23. }
  24. }

Just like the controllers, view components also follow a convention for naming classes. This means that you can create a view component by adding the suffix “ViewComponent” to your class. Adding to that VCs must be public, non-nested and non-abstract classes.
Notes You can also use the [ViewComponent] attribute in your class when referencing a ViewComponent.
You can use the overload method of the View() to specify a view to render from your InvokeAsync method. For example return View(“YourViewName”,model).
The InvokeAsync exposes a method that can be called from a view and it can take an arbitrary number of arguments. As you have seen from the code above, we passed in the parameter “type” in the method to filter the data.
Now let's add the view for the View Component that we just have created. Keep in mind that VC follows a convention too when referencing views. So the first thing to do is to create a new folder within the Home folder. The folder name must be “Components”. Now since we followed the convention in our example, the next thing to do is to create another new folder within the Components folder, this time the folder name must match your class name minus the “ViewComponents” suffix. In this case name the folder “HeroList”. Finally add a new view within the HeroList folder and name it “Default” since we didn't specify the view to render in our InvokeAsync code. Your project structure should now look like the following:

Figure 11: Solution Explorer    In your Default.cshtml file, add the following markup for your View Component's view:

  1. @model IEnumerable<MVC6Demo.Models.DOTAHero>
  2. <h3>Strength Heroes</h3>
  3. <ul>
  4. @foreach (var p in Model)
  5. {
  6. <li>@p.Name</li>
  7. }
  8. </ul>

And here's how we call the View Component from the main view (Index.cshmtl):

  1. <div>
  2. @await Component.InvokeAsync("HeroList", "strength")
  3. </div>

Running the page will display the following output:

Figure 12: Final Output
That's simple! I hope you will find this article useful. Stay tuned for more!

[转] asp.net core Introducing View Components相关推荐

  1. asp.net core mvc View Component 应用

    ViewComponent 1.View 组件介绍 在ASP.NET CORE MVC中,View组件有点类似于partial views,但是他们更强大,View组件不能使用model bindin ...

  2. ASP.NET Core 运行原理剖析

    1.1. 概述 在ASP.NET Core之前,ASP.NET Framework应用程序由IIS加载.Web应用程序的入口点由InetMgr.exe创建并调用托管.以初始化过程中触发HttpAppl ...

  3. ASP.NET Core MVC 之视图组件(View Component)

    1.视图组件介绍 视图组件是 ASP.NET Core MVC 的新特性,类似于局部视图,但它更强大.视图组件不使用模型绑定,并且仅依赖于调用它时所提供的数据. 视图组件特点: 呈块状,而不是整个响应 ...

  4. 移花接木:借助 IViewLocationExpander 更换 ASP.NET Core View Component 视图路径

    端午节在家将一个 asp.net 项目向 asp.net core 迁移时遇到了一个问题,用 view component 取代 Html.RenderAction 之后,运行时 view compo ...

  5. 007.Adding a view to an ASP.NET Core MVC app -- 【在asp.net core mvc中添加视图】

    索引: 目录索引 Adding a view to an ASP.NET Core MVC app 在asp.net core mvc中添加视图 2017-3-4 7 分钟阅读时长 本文内容 1.Ch ...

  6. Asp.Net Core 入门(四)—— Model、View、Controller

    和我们学习Asp.Net MVC一样,Asp.Net Core MVC的Model.View.Controller也和我们熟悉的Asp.Net MVC中的相似.不同的是我们在使用Asp.Net Cor ...

  7. ASP.NET Core MVC 之依赖注入 View

    ASP.NET Core 支持在试图中使用依赖注入.这将有助于提供视图专用的服务,比如本地化或者仅用于填充视图元素的数据.应尽量保持控制器和视图之间的关注点分离.视图所显示的大部分数据应该从控制器传入 ...

  8. ASP.NET Core重写个人博客站点小结

    今天用ASP.NET Core重写了个人博客站点,原来是基于ASP.NET 4.5开发的.重写工作总体很顺利,最后成功发布到Ubunt+Nginx平台上.效果如下: 右边的Header信息里可以看到已 ...

  9. ASP.NET MVC升级到ASP.NET Core MVC踩坑小结

    写在前面 ASP.NET Core是微软新推出的支持跨平台.高性能.开源的开发框架,它的优势不必多说,因为已经说得太多了.当然,现在依然有着数量庞大的系统运行于.NET Framework上,由于有大 ...

最新文章

  1. 在LinearLayout中嵌套RelativeLayout来设置Button的位置(xml文件)
  2. Java EE (11) - 影响性能的因素
  3. LeetCode Integer to Roman(数字转罗马)
  4. c语言关键字-static
  5. 中兴执行副总裁熊辉回母校谈目标:相信总有路可走
  6. Maven发布jar包到Nexus私库
  7. easyui js解析字符串_js相关:详解Jquery Easyui的验证扩展
  8. path.join 与 path.resolve 的区别
  9. (王道408考研操作系统)第四章文件管理-第二节4:磁盘的管理
  10. 利用Postman测试智慧交通系统接口
  11. RabbitMQ入门指南二(Java)
  12. 回天科技工程师房工对十大硬盘数据恢复软件简评!
  13. 关于PCB板热设计的学习总结
  14. Appfuse 开发环境搭建
  15. 计算机切换用户屏幕闪,小编教您Win10切换用户后闪屏的具体办法
  16. 每日excel学习之查找替换和定位
  17. 新浪微博生成超短链接
  18. 奖金100万!北大“韦神”,获奖了!
  19. 安卓推送、android文本推送、安卓富媒体推送解决方案
  20. CSAPP LAB4 键盘驱动程序的分析与修改(谢罪)

热门文章

  1. 基于winpcap开发的相关资料
  2. Vue+DataTables warning:table id=xxxx -Cannot reinitialize DataTable.报错解决方法
  3. 如何用Node调用腾讯AI图像服务
  4. 图的遍历——广度优先搜索(Breadth First Search)
  5. HACMP 认证学习系列,第 2 部分-1:计划与设计
  6. WEB应用数据验证指南
  7. html页面转换成pdf
  8. ASP.NET网络编程中常用到的27个函数集
  9. 手把手带你撸一个cli工具
  10. HashMap、HashTable、ConcurrentHashMap、HashSet区别 线程安全类