本文转自: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. Winform 数据库连接配置界面

    一.添加引用       C:\Program Files\Microsoft Visual Studio 8\Common7\IDE\Microsoft.Data.ConnectionUI.Dial ...

  2. [SinGuLaRiTy] 分治题目复习

    [SInGuLaRiTy-1025] Copyrights (c) SinGuLaRiTy 2017. All Rights Reserved. [POJ 1905] 棍的膨胀 (Expanding ...

  3. JavaScript之入门篇(二)

    终于学到后面的语法部分了,感觉这门语言基础部分和当初学习VB的时候感觉一样一样的,章节目录让我不禁又想到了VB课本.由于怕学过了,过段时间忘了,于是,大概总结一下. 数据类型部分 ① Typeof操作 ...

  4. 装饰器设计模式初探及Java中实际应用举例

    本篇随笔主要介绍用Java实现简单的装饰器设计模式: 先来看一下装饰器设计模式的类图:  从图中可以看到,我们可以装饰Component接口的任何实现类,而这些实现类也包括了装饰器本身,装饰器本身也可 ...

  5. django2集成DjangoUeditor富文本编辑器

    富文本编辑器,在web开发中可以说是不可缺少的.django并没有自带富文本编辑器,因此我们需要自己集成,在这里推荐大家使用DjangoUeditor,因为DjangoUeditor封装了我们需要的一 ...

  6. Exadata X7来了

    1.Exadata Brings In-Memory OLTP to Storage Exadata Storage Servers add a memory cache in front of Fl ...

  7. json几种读取方式,ArrayList循环读取【转】

    在之前写过提取json数据格式的文章,这次对jmeter读取json数据格式进行整理. 举例一个接口的response 格式如下: { "data" : { "devic ...

  8. chkconfig命令详细介绍

    命令介绍 chkconfig命令用来更新.查询.修改不同运行级上的系统服务.比如安装了httpd服务,并且把启动的脚本放在了/etc/rc.d/init.d目录下,有时候需要开机自动启动它,而有时候则 ...

  9. HDU - 1878 欧拉回路 (连通图+度的判断)

    欧拉回路是指不令笔离开纸面,可画过图中每条边仅一次,且可以回到起点的一条回路.现给定一个图,问是否存在欧拉回路? Input 测试输入包含若干测试用例.每个测试用例的第1行给出两个正整数,分别是节点数 ...

  10. Go语言基础之19--web编程基础

    一.web编程基础 1.1 web工作方式 1.2 HTTP协议详解 a.http 请求包体 GET /domains/example/ HTTP/1.1 //请求行: 请求方法 请求URI HTTP ...