本文转自: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. 从零开始搭建.NET Core 2.0 API(学习笔记一)

    从零开始搭建.NET Core 2.0 API(学习笔记一) 一. VS 2017 新建一个项目 选择ASP.NET Core Web应用程序,再选择Web API,选择ASP.NET Core 2. ...

  2. wffmpeg64.dll调用 尝试读取或写入受保护的内存。这通常指示其他内存已损坏。

    求解中.....

  3. 读取word

    目标:将word中数据转存到数据库 实质:数据的读写 难点:word文件格式的处理(识别,读取),/ 提取word有效的相关字段 实现: 1.基础了解:word(文字处理应用程序/文档工具)软件   ...

  4. json互转笔记

    JSON.parse()与JSON.stringify()的区别 JSON.parse()[从一个字符串中解析出json对象] 例子: //定义一个字符串 var data='{"name& ...

  5. 【转】windows server 2012 R2搭建IIS服务器

    源地址:http://blog.csdn.net/microsoft_wu/article/details/46521017

  6. 图片压缩工具之grunt-contrib-imagemin

    对页面进行优化时~免不了对使用的图片进行压缩~以便减小我们使用的图片的大小~这样就可以减少用户下载的文件大小,加快页面访问速度.Google Pagespeed最佳实践建议我们用 jpegtran 或 ...

  7. [比赛|考试]nowcoder 小白月赛7

    牛客小白月赛7 比赛地址.本次比赛我切了8道(ACM赛制),rank(20). 反思:刚入手ACM赛,光追求刺激了,没有总结ACM赛制的经验.是应该多提交>..还是少提交...小白赛还有两道不会 ...

  8. Docker部署MySql应用

    1. MySQL部署 1.1拉取MySQL镜像 docker pull mysql 查看镜像 docker images 1.2创建MySQL容器 docker run -di --name piny ...

  9. [題解](最小生成樹)luogu_P2916安慰奶牛

    可以發現每個點經過次數恰好等於這個點的度數,所以把點權下放邊權,跑最小生成樹,原來邊權乘二在加上兩端點權,答案再加一遍起點最小點權 #include<bits/stdc++.h> #def ...

  10. POJ1049 Microprocessor Simulation

    题目来源:http://poj.org/problem?id=1049 题目大意: 一种小型的微处理器有以下特性: 1. 每个字长4bit. 2. 地址用2个字进行编码.先高位字后低位字,即高位字大的 ...