[转] 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!
posted on 2017-01-13 12:20 freeliver54 阅读(288) 评论(0) 编辑 收藏 举报