[转] asp.net core Introducing View Components
本文转自: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:
- using Microsoft.AspNet.Mvc;
- using System.Threading.Tasks;
- using MVC6Demo.Models;
- using System.Collections.Generic;
- namespace MVC6Demo.ViewComponents
- {
- public class HeroListViewComponent: ViewComponent
- {
- public async Task < IViewComponentResult > InvokeAsync(string type)
- {
- var heroes = await GetHeroesAsync(type);
- return View(heroes);
- }
- private Task < IEnumerable < DOTAHero >> GetHeroesAsync(string type)
- {
- return Task.FromResult(GetHeroes(type));
- }
- private IEnumerable < DOTAHero > GetHeroes(string type)
- {
- HeroManager HM = new HeroManager();
- return HM.GetHeroesByType(type);
- }
- }
- }
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:
- @model IEnumerable<MVC6Demo.Models.DOTAHero>
- <h3>Strength Heroes</h3>
- <ul>
- @foreach (var p in Model)
- {
- <li>@p.Name</li>
- }
- </ul>
And here's how we call the View Component from the main view (Index.cshmtl):
- <div>
- @await Component.InvokeAsync("HeroList", "strength")
- </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的更多相关文章
- asp.net core mvc View Component 应用
ViewComponent 1.View 组件介绍 在ASP.NET CORE MVC中,View组件有点类似于partial views,但是他们更强大,View组件不能使用model bindin ...
- 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: ...
- 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 ...
- 移花接木:借助 IViewLocationExpander 更换 ASP.NET Core View Component 视图路径
端午节在家将一个 asp.net 项目向 asp.net core 迁移时遇到了一个问题,用 view component 取代 Html.RenderAction 之后,运行时 view compo ...
- ASP.NET Core MVC 之视图组件(View Component)
1.视图组件介绍 视图组件是 ASP.NET Core MVC 的新特性,类似于局部视图,但它更强大.视图组件不使用模型绑定,并且仅依赖于调用它时所提供的数据. 视图组件特点: 呈块状,而不是整个响应 ...
- 创建ASP.NET Core MVC应用程序(1)-添加Controller和View
创建ASP.NET Core MVC应用程序(1)-添加Controller和View 参考文档:Getting started with ASP.NET Core MVC and Visual St ...
- ASP.NET Core开发-MVC 使用dotnet 命令创建Controller和View
使用dotnet 命令在ASP.NET Core MVC 中创建Controller和View,之前讲解过使用yo 来创建Controller和View. 下面来了解dotnet 命令来创建Contr ...
- [译]View components and Inject in ASP.NET MVC 6
原文:http://www.asp.net/vnext/overview/aspnet-vnext/vc 介绍view components view components (VCs) 类似于part ...
- ASP.NET 5系列教程 (三):view components介绍
在ASP.NET MVC 6中,view components (VCs) 功能类似于虚拟视图,但是功能更加强大. VCs兼顾了视图和控制器的优点,你可以把VCs 看作一个Mini 控制器.它负责控制 ...
随机推荐
- String类-小用
字符串-string (1)string在Java中是一个引用类型,string变量可引用一个字符串对象 (2) 例1: s0,s1,s2引用同一个对象 New创建的两个string是不同的对象只是内 ...
- Quick Reference Card Urls For Web Developer
C# C# Cheatsheet & Notes Coding Guidelines for C# 3.0, 4.0, 5.0 Core C# and .NET Quick Reference ...
- MySQL的ODBC安装错误问题!
MySQL的ODBC安装时候可能会出错,主要原因是缺少VC支持库,需要2010版本的VC支持库!!X86和X64分别对应MySQL对应的ODBC,不能安装一个两个都搞定,如果需要安装两个ODBC驱动, ...
- 【bzoj3122】: [Sdoi2013]随机数生成器 数论-BSGS
[bzoj3122]: [Sdoi2013]随机数生成器 当a>=2 化简得 然后 BSGS 求解 其他的特判 : 当 x=t n=1 当 a=1 当 a=0 判断b==t /* http: ...
- ASP 注释
ASP.NET中使用HTML标准注释<!-- -->回导致程序崩溃,必须使用ASP标准注释<%-- --%>
- Apache虚拟主机-解惑篇
有很多平时喜欢钻研的童鞋会发现,为什么有时候自己访问某XXse网站时,总是更新IP地址,内容却与以前一样.这个时候就要了解虚拟主机的概念了.了解这个概念,能够帮助运维同学,更内涵的隐藏自己的主 ...
- vue file-loader vs url-loader
1.前言 如果我们希望在页面引入图片(包括img的src和background的url).当我们基于webpack进行开发时,引入图片会遇到一些问题. 其中一个就是引用路径的问题.拿backgroun ...
- Python——用os模块寻找指定目录(包括子目录)下所有图片文件
import os # 导入os模块 def search_file(start_dir): img_list = [] extend_name = ['.jpg', '.png', '.gif'] ...
- mysql的时区错误问题
JDBC连接数据库报错如下: java.sql.SQLException: The server time zone value 'Öйú±ê׼ʱ¼ä' is unrecognized or r ...
- 前端基础------jquer y学习
一. jquery是什么 快速,简洁,轻量级的JavaScript库(JavaScript框架)使用户可以快速的操作HTML document,实现动画效果,并方便的地为网站提供AJAX交互.文档全面 ...