本文转自: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: Getting Started with ASP.NET MVC Core

    1. ASP.NET Core the Unified Framework ASP.NET Core的统一框架 2. New Solution Project 新的解决方案项目 src folder: ...

  3. Introducing ASP.NET Core: The New ASP.NET in Town!

    The new version of ASP.NET is called ASP.NET Core (a.k.a ASP.NET 5) and it has the most significant ...

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

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

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

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

  6. 创建ASP.NET Core MVC应用程序(1)-添加Controller和View

    创建ASP.NET Core MVC应用程序(1)-添加Controller和View 参考文档:Getting started with ASP.NET Core MVC and Visual St ...

  7. ASP.NET Core开发-MVC 使用dotnet 命令创建Controller和View

    使用dotnet 命令在ASP.NET Core MVC 中创建Controller和View,之前讲解过使用yo 来创建Controller和View. 下面来了解dotnet 命令来创建Contr ...

  8. [译]View components and Inject in ASP.NET MVC 6

    原文:http://www.asp.net/vnext/overview/aspnet-vnext/vc 介绍view components view components (VCs) 类似于part ...

  9. ASP.NET 5系列教程 (三):view components介绍

    在ASP.NET MVC 6中,view components (VCs) 功能类似于虚拟视图,但是功能更加强大. VCs兼顾了视图和控制器的优点,你可以把VCs 看作一个Mini 控制器.它负责控制 ...

随机推荐

  1. tomcat Linux安装

    tomcat依赖jdk软件包,所以先配置jdk. 配置完成后 将下载好的tomcat 解压至/usr/local tar -xvf apache-tomcat-7.0.70.tar.gz -C /us ...

  2. Binder学习笔记(十二)—— binder_transaction(...)都干了什么?

    binder_open(...)都干了什么? 在回答binder_transaction(...)之前,还有一些基础设施要去探究,比如binder_open(...),binder_mmap(...) ...

  3. (原创)E - Straight Shot Gym - 101652R

    解题思路:这道题的题意就是给你n,总距离X,速度v:以及n组数据:人行道的左端点和右端点,以及人行道的速度(竖直方向),如果从(0,0)到(X,0)的时间小于2X/v,则输出其时间,否则输出”Too ...

  4. 微信网页授权操作逻辑封装-C#实例

    http://blog.csdn.net/u011127019/article/details/52650619

  5. 第一章:初识JAVA

    一:计算机语言发展史 机器语言:典型的二进制文件和计算机交流. 汇编语言: 通过大量的标识符表示一些基本操作来和计算机做交流. 高级语言:通过常见的英语指令来编写程序,完成沟通 常见高级语言 Java ...

  6. linux线程切换和进程切换

    进程切换分两步: 1.切换页目录以使用新的地址空间 2.切换内核栈和硬件上下文 对于linux来说,线程和进程的最大区别就在于地址空间,对于线程切换,第1步是不需要做的,第2是进程和线程切换都要做的. ...

  7. kuangbin专题十六 KMP&&扩展KMP HDU2609 How many (最小字符串表示法)

    Give you n ( n < 10000) necklaces ,the length of necklace will not large than 100,tell me How man ...

  8. Redis学习笔记(2)—— Redis的安装和使用

    一.CentOS安装Redis 1.1 安装环境 redis是C语言开发的,安装redis需要先将官网下载的源码进行编译,编译依赖gcc环境.如果没有gcc环境,需要安装gcc: yum instal ...

  9. window 系统 修改服务器远程登录端口

    window 系统 [ 默认3389远程端口 ] 快捷键:Ctrl+R  然后输入“regedit”,打开注册表 或者 单击左下角[开始]——[运行],然后在输入框输入 regedit,点击确定,打开 ...

  10. 利用SharePoint项目改造的Web项目问题——Windows身份验证

    最近领导交给一个项目:改造现有的SharePoint项目.UI层是做好的,只需要把实现的所有接口方法重新实现一遍,改造成Web版的实现方式. 现在要做基于Windows身份认证的登陆: 配置IIS—— ...