diff --git a/OdeToFoodRider/OdeToFoodRider/Controllers/HomeController.cs b/OdeToFoodRider/OdeToFoodRider/Controllers/HomeController.cs index d9619e9..bb45738 100644 --- a/OdeToFoodRider/OdeToFoodRider/Controllers/HomeController.cs +++ b/OdeToFoodRider/OdeToFoodRider/Controllers/HomeController.cs @@ -1,6 +1,7 @@ using Microsoft.AspNetCore.Mvc; using OdeToFoodRider.Models; using OdeToFoodRider.Services; +using OdeToFoodRider.ViewModels; namespace OdeToFoodRider.Controllers { @@ -8,16 +9,22 @@ 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); } } diff --git a/OdeToFoodRider/OdeToFoodRider/ViewModels/HomeIndexViewModel.cs b/OdeToFoodRider/OdeToFoodRider/ViewModels/HomeIndexViewModel.cs new file mode 100644 index 0000000..c905243 --- /dev/null +++ b/OdeToFoodRider/OdeToFoodRider/ViewModels/HomeIndexViewModel.cs @@ -0,0 +1,11 @@ +using System.Collections.Generic; +using OdeToFoodRider.Models; + +namespace OdeToFoodRider.ViewModels +{ + public class HomeIndexViewModel + { + public IEnumerable Restaurants { get; set; } + public string CurrentMessage { get; set; } + } +} \ No newline at end of file diff --git a/OdeToFoodRider/OdeToFoodRider/Views/Home/Index.cshtml b/OdeToFoodRider/OdeToFoodRider/Views/Home/Index.cshtml index 7cfe4e0..e42ce5d 100644 --- a/OdeToFoodRider/OdeToFoodRider/Views/Home/Index.cshtml +++ b/OdeToFoodRider/OdeToFoodRider/Views/Home/Index.cshtml @@ -1,5 +1,5 @@ @using OdeToFoodRider.Models -@model IEnumerable +@model OdeToFoodRider.ViewModels.HomeIndexViewModel @@ -8,11 +8,11 @@ My title +

@Model.CurrentMessage

@* VSRD: No table snippet in Rider. *@ - @* VSRD: No foreach live template here, just keyword completion like in Visual Studio *@ - @foreach (var restaurant in Model) + @foreach (var restaurant in Model.Restaurants) { diff --git a/OdeToFoodVisualStudio/OdeToFoodVisualStudio/Controllers/HomeController.cs b/OdeToFoodVisualStudio/OdeToFoodVisualStudio/Controllers/HomeController.cs index 3bb9d37..169f0e3 100644 --- a/OdeToFoodVisualStudio/OdeToFoodVisualStudio/Controllers/HomeController.cs +++ b/OdeToFoodVisualStudio/OdeToFoodVisualStudio/Controllers/HomeController.cs @@ -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); diff --git a/OdeToFoodVisualStudio/OdeToFoodVisualStudio/ViewModels/HomeIndexViewModel.cs b/OdeToFoodVisualStudio/OdeToFoodVisualStudio/ViewModels/HomeIndexViewModel.cs new file mode 100644 index 0000000..336f404 --- /dev/null +++ b/OdeToFoodVisualStudio/OdeToFoodVisualStudio/ViewModels/HomeIndexViewModel.cs @@ -0,0 +1,11 @@ +using OdeToFoodVisualStudio.Models; +using System.Collections.Generic; + +namespace OdeToFoodVisualStudio.ViewModels +{ + public class HomeIndexViewModel + { + public IEnumerable Restaurants { get; set; } + public string CurrentMessage { get; set; } + } +} diff --git a/OdeToFoodVisualStudio/OdeToFoodVisualStudio/Views/Home/Index.cshtml b/OdeToFoodVisualStudio/OdeToFoodVisualStudio/Views/Home/Index.cshtml index f7264d5..312912e 100644 --- a/OdeToFoodVisualStudio/OdeToFoodVisualStudio/Views/Home/Index.cshtml +++ b/OdeToFoodVisualStudio/OdeToFoodVisualStudio/Views/Home/Index.cshtml @@ -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 +@model OdeToFoodVisualStudio.ViewModels.HomeIndexViewModel + +

@Model.CurrentMessage

@* VSRD: Table generated in VS with a "table" code snippet *@
@restaurant.Id
@* VSRD: No 'foreach' code snippet in VS, just keyword completion *@ - @foreach (var restaurant in Model) + @foreach (var restaurant in Model.Restaurants) {
@restaurant.Id