Notes from building a first ASP.Net Core App (part 10)

There’s a new feature called Tag Helpers. They are attributes that you add that help clean up the html tags. They are actually executed just like yellow code, on the server. I’m going to set up a simple one to show redirection to a separate page.

  1. In the Home folder, add a new MVC View Page called Detail.cshtml. This will simply display the details for a single Hotel. Add the following code:
    @model MyFirstAspNetCoreApp.Models.Hotel
    
    <html>
    <head>
        <title>Home</title>
    </head>
    <body>
    <h1>Welcome!</h1>
        @Model.Id <br/>
        @Model.DisplayName
    </body>
    </html>
    
  2. Now we need to add a method to the Home Controller so that knows about the Detail page. In the Home Controller, add the Detail method, as follows:
    using Microsoft.AspNetCore.Mvc;
    using MyFirstAspNetCoreApp.Services;
    using System.Linq;
    
    namespace MyFirstAspNetCoreApp.Controllers
    {
        public class HomeController : Controller
        {
            private IHotelDataService hotelData;
    
            public HomeController(IHotelDataService hotelData)
            {
                this.hotelData = hotelData;
            }
            public IActionResult Index()
            {
                var model = hotelData.GetAll();
                return View(model);
            }
    
            public IActionResult Detail(int id)
            {
                var model = hotelData.GetAll().First(h => h.Id == id);
                return View(model);
            }
    
        }
    }
    

    Note that I’ve cheated a little with the use of First in the LINQ above. It’s a sample app and I need to keep it as simple as possible.

  3. Next we need to add a Tag Helper dependency to our project. Right-click on references and select the Nuget Package Manager. Choose browse and in the search box type TagHelpers. Select the Microsoft.AspNetCode.Mvc.TagHelpers package, install the latest version and blindly accept the licence. After installing the package, there will now be an extra dependency in the project.json file for the newly installed package.
  4. There is a new type of file you can use in Asp.Net now called a View Imports file. That file is called _ViewImports.cshtml. You can add import statements to that file and they will be imported by every file in that folder or in any subfolders. Right-click on the Views folder, and add a new MVC View Imports Page called _ViewImports.cshtml (the default.)
  5. Inside the _ViewImports.cshtml file I add the following line of code, which tells the application that I will be using all the tags in the current assembly:
    @addTagHelper "*, Microsoft.AspNetCore.Mvc.TagHelpers"
    
  6. Now, to keep things simple, I am going to change the DisplayName being presented in the Home Index.cshtml page to a link. In the Home Index.cshtml, I change the contents of the li tag as follows:

    @model IEnumerable<MyFirstAspNetCoreApp.Models.Hotel>
    
    <html>
    <head>
        <title>Home</title>
    </head>
    <body>
        <h1>Welcome!</h1>
        <ul>
            @foreach (var hotel in Model)
            {
            <li>
                <a asp-controller="Home" asp-action="Detail" asp-route-id="@hotel.Id">@hotel.DisplayName</a>
            </li>
            }
        </ul>
    </body>
    </html>
    

    Interestingly, you don’t actually need to specify the asp-controller tag if that’s the controller you are currently in. Also, this doesn’t mean that the old ActionLink methods aren’t still available. The following line would still work, it just doesn’t look as aesthetically appealing:

    @Html.ActionLink(@Model.Id.ToString(), "Detail", new { id = Model.Id })
    
  7. Run the application (F5) and when you hover over the links, you should see the last argument (id) change in the link. Click on one of the links, and you should see the details of that link displayed on the Detail page.

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: