Create a view

Views in ASP.NET Core are built using the Razor templating language, which combines HTML and C# code. (If you’ve written pages using Jade/Pug or Handlebars moustaches in JavaScript, ERB in Ruby on Rails, or Thymeleaf in Java, you’ve already got the basic idea.)

Most view code is just HTML, with the occasional C# statement added in to pull data out of the view model and turn it into text or HTML. The C# statements are prefixed with the @ symbol.

The view rendered by the Index action of the TodoController needs to take the data in the view model (an array of to-do items) and display it as a nice table for the user. By convention, views are placed in the Views directory, in a subdirectory corresponding to the controller name. The file name of the view is the name of the action with a .cshtml extension.

Views/Todo/Index.cshtml

  1. @model TodoViewModel
  2. @{
  3. ViewData["Title"] = "Manage your todo list";
  4. }
  5. <div class="panel panel-default todo-panel">
  6. <div class="panel-heading">@ViewData["Title"]</div>
  7. <table class="table table-hover">
  8. <thead>
  9. <tr>
  10. <td>&#x2714;</td>
  11. <td>Item</td>
  12. <td>Due</td>
  13. </tr>
  14. </thead>
  15. @foreach (var item in Model.Items)
  16. {
  17. <tr>
  18. <td>
  19. <input type="checkbox" name="@item.Id" value="true" class="done-checkbox">
  20. </td>
  21. <td>@item.Title</td>
  22. <td>@item.DueAt</td>
  23. </tr>
  24. }
  25. </table>
  26. <div class="panel-footer add-item-form">
  27. <form>
  28. <div id="add-item-error" class="text-danger"></div>
  29. <label for="add-item-title">Add a new item:</label>
  30. <input id="add-item-title">
  31. <button type="button" id="add-item-button">Add</button>
  32. </form>
  33. </div>
  34. </div>

At the very top of the file, the @model directive tells Razor which model to expect this view to be bound to. The model is accessed through the Model property.

Assuming there are any to-do items in Model.Items, the foreach statement will loop over each to-do item and render a table row (<tr> element) containing the item’s name and due date. A checkbox is also rendered that contains the item’s ID, which you’ll use later to mark the item as completed.

The layout file

You might be wondering where the rest of the HTML is: what about the <body> tag, or the header and footer of the page? ASP.NET Core uses a layout view that defines the base structure that the rest of the views are rendered inside of. It’s stored in Views/Shared/_Layout.cshtml.

The default ASP.NET Core template includes Bootstrap and jQuery in this layout file, so you can quickly create a web application. Of course, you can use your own CSS and JavaScript libraries if you’d like.

Customizing the stylesheet

For now, just add these CSS style rules to the bottom of the site.css file:

wwwroot/css/site.css

  1. div.todo-panel {
  2. margin-top: 15px;
  3. }
  4. table tr.done {
  5. text-decoration: line-through;
  6. color: #888;
  7. }

You can use CSS rules like these to completely customize how your pages look and feel.

ASP.NET Core and Razor can do much more, such as partial views and server-rendered view components, but a simple layout and view is all you need for now. The official ASP.NET Core documentation (at https://docs.asp.net) contains a number of examples if you’d like to learn more.