Skip to content

Commit

Permalink
Creating and using a view model for the Index view
Browse files Browse the repository at this point in the history
  • Loading branch information
gorohoroh committed Jul 4, 2018
1 parent 47fe289 commit fde46ac
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 9 deletions.
11 changes: 9 additions & 2 deletions OdeToFoodRider/OdeToFoodRider/Controllers/HomeController.cs
Original file line number Diff line number Diff line change
@@ -1,23 +1,30 @@
using Microsoft.AspNetCore.Mvc;
using OdeToFoodRider.Models;
using OdeToFoodRider.Services;
using OdeToFoodRider.ViewModels;

namespace OdeToFoodRider.Controllers
{
public class HomeController : Controller
{
// VSRD: This time, "Initialize field from constructor" does exactly what we want.
IRestaurantData _restaurantData;
IGreeter _greeter;

public HomeController(IRestaurantData restaurantData)
public HomeController(IRestaurantData restaurantData, IGreeter greeter)
{
_restaurantData = restaurantData;
_greeter = greeter;
}


public IActionResult Index()
{
var model = _restaurantData.GetAll();
// VSRD: "Use object initialization" available in both IDEs but in Visual Studio, it's only available on the constructor call, not on further variable usages.
var model = new HomeIndexViewModel();
model.Restaurants = _restaurantData.GetAll();
model.CurrentMessage = _greeter.GetMessageOfTheDay();

return View(model);
}
}
Expand Down
11 changes: 11 additions & 0 deletions OdeToFoodRider/OdeToFoodRider/ViewModels/HomeIndexViewModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using System.Collections.Generic;
using OdeToFoodRider.Models;

namespace OdeToFoodRider.ViewModels
{
public class HomeIndexViewModel
{
public IEnumerable<Restaurant> Restaurants { get; set; }
public string CurrentMessage { get; set; }
}
}
6 changes: 3 additions & 3 deletions OdeToFoodRider/OdeToFoodRider/Views/Home/Index.cshtml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
@using OdeToFoodRider.Models
@model IEnumerable<Restaurant>
@model OdeToFoodRider.ViewModels.HomeIndexViewModel

<!DOCTYPE html>

Expand All @@ -8,11 +8,11 @@
<title>My title</title>
</head>
<body>
<h1>@Model.CurrentMessage</h1>
@* VSRD: No table snippet in Rider. *@
<table>

@* VSRD: No foreach live template here, just keyword completion like in Visual Studio *@
@foreach (var restaurant in Model)
@foreach (var restaurant in Model.Restaurants)
{
<tr>
<td>@restaurant.Id</td>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,26 +1,31 @@
using Microsoft.AspNetCore.Mvc;
using OdeToFoodVisualStudio.Models;
using OdeToFoodVisualStudio.Services;
using OdeToFoodVisualStudio.ViewModels;

namespace OdeToFoodVisualStudio.Controllers
{
public class HomeController : Controller
{
private IRestaurantData _restaurantData;
private IGreeter _greeter;

// VSRD: Visual Studio doesn't have import items in completion, which means that here and in other cases when referencing an unimported type, you have to make sure to spell
// and capitalize it correctly, and then use a quick action to add an import. In Rider, import items are available in completion, which allows using camelHumps and abbreviations
// without being precise with naming, and additionally, accepting an import symbol suggestion adds the necessary using statement without the need to explicitly invoke a quick action.
public HomeController(IRestaurantData restaurantData)
public HomeController(IRestaurantData restaurantData, IGreeter greeter)
{
// VSRD: VS provides a set of quick actions to generate _restaurantData (as a full or read-only field, full or read-only property, local variable), as well as explicit actions
// to change _restaurantData to IRestaurantData or restaurantData
_restaurantData = restaurantData;
_greeter = greeter;
}

public IActionResult Index()
{
var model = _restaurantData.GetAll();
var model = new HomeIndexViewModel();
model.Restaurants = _restaurantData.GetAll();
model.CurrentMessage = _greeter.GetMessageOfTheDay();

return View(model);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using OdeToFoodVisualStudio.Models;
using System.Collections.Generic;

namespace OdeToFoodVisualStudio.ViewModels
{
public class HomeIndexViewModel
{
public IEnumerable<Restaurant> Restaurants { get; set; }
public string CurrentMessage { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
@* VSRD: Visual Studio doesn't suggest importing the Restaurant model when typing @model Restaurant, so you have to type in the FQN. Let's see what Rider can do here :) *@
@model IEnumerable<OdeToFoodVisualStudio.Models.Restaurant>
@model OdeToFoodVisualStudio.ViewModels.HomeIndexViewModel

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
</head>
<body>

<h1>@Model.CurrentMessage</h1>
@* VSRD: Table generated in VS with a "table" code snippet *@
<table>
@* VSRD: No 'foreach' code snippet in VS, just keyword completion *@
@foreach (var restaurant in Model)
@foreach (var restaurant in Model.Restaurants)
{
<tr>
<td>@restaurant.Id</td>
Expand Down

0 comments on commit fde46ac

Please sign in to comment.