Skip to content

Commit

Permalink
Adding the Edit form using Razor Pages
Browse files Browse the repository at this point in the history
  • Loading branch information
gorohoroh committed Jul 12, 2018
1 parent e7d3fbb commit d721c76
Show file tree
Hide file tree
Showing 12 changed files with 169 additions and 0 deletions.
25 changes: 25 additions & 0 deletions OdeToFoodRider/OdeToFoodRider/Pages/Restaurants/Edit.cshtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
@page "{id}"
@using OdeToFoodRider.Pages.Restaurants

@model EditModel

<h1>Edit @Model.Restaurant.Name</h1>

<form method="post">

<input type="hidden" asp-for="Restaurant.Id"/>

<div>
<label asp-for="Restaurant.Name"></label>
<input asp-for="Restaurant.Name"/>
<span asp-validation-for="Restaurant.Name"></span>
</div>

<div>
<label asp-for="Restaurant.Cuisine"></label>
<select asp-for="Restaurant.Cuisine" asp-items="@Html.GetEnumSelectList<CuisineType>()"></select>
<span asp-validation-for="Restaurant.Cuisine"></span>
</div>

<input type="submit" name="save" value="Save"/>
</form>
42 changes: 42 additions & 0 deletions OdeToFoodRider/OdeToFoodRider/Pages/Restaurants/Edit.cshtml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using OdeToFoodRider.Models;
using OdeToFoodRider.Services;

namespace OdeToFoodRider.Pages.Restaurants
{
public class EditModel : PageModel
{
private readonly IRestaurantData _restaurantData;

[BindProperty]
public Restaurant Restaurant { get; set; }

public EditModel(IRestaurantData restaurantData)
{
_restaurantData = restaurantData;
}

public IActionResult OnGet(int id)
{
Restaurant = _restaurantData.Get(id);
if (Restaurant == null)
{
return RedirectToAction("Index", "Home");
}

return Page();
}

public IActionResult OnPost()
{
if (ModelState.IsValid)
{
_restaurantData.Update(Restaurant);
return RedirectToAction("Details", "Home", new {id = Restaurant.Id});
}

return Page();
}
}
}
1 change: 1 addition & 0 deletions OdeToFoodRider/OdeToFoodRider/Services/IRestaurantData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@ public interface IRestaurantData
IEnumerable<Restaurant> GetAll();
Restaurant Get(int id);
Restaurant Add(Restaurant restaurant);
Restaurant Update(Restaurant restaurant);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,10 @@ public Restaurant Add(Restaurant restaurant)
_restaurants.Add(restaurant);
return restaurant;
}

public Restaurant Update(Restaurant restaurant)
{
throw new System.NotImplementedException();
}
}
}
8 changes: 8 additions & 0 deletions OdeToFoodRider/OdeToFoodRider/Services/SqlRestaurantData.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Collections.Generic;
using System.Linq;
using Microsoft.EntityFrameworkCore;
using OdeToFoodRider.Data;
using OdeToFoodRider.Models;

Expand All @@ -21,6 +22,13 @@ public Restaurant Add(Restaurant restaurant)
return restaurant;
}

public Restaurant Update(Restaurant restaurant)
{
_context.Attach(restaurant).State = EntityState.Modified;
_context.SaveChanges();
return restaurant;
}

public Restaurant Get(int id)
{
return _context.Restaurants.FirstOrDefault(r => r.Id == id);
Expand Down
1 change: 1 addition & 0 deletions OdeToFoodRider/OdeToFoodRider/Views/Home/Index.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
<a href="/home/details/@restaurant.Id">Details</a>
@Html.ActionLink("Go", "Details", new {id = restaurant.Id})
<a asp-action="Details" asp-route-id="@restaurant.Id">More</a>
<a asp-page="/Restaurants/Edit" asp-route-id="@restaurant.Id">Edit</a>
</td>
</tr>
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
@page "{id}"
@using OdeToFoodVisualStudio.Pages.Restaurants

@model EditModel

<h1>Edit @Model.Restaurant.Name</h1>

<form method="post">

<input type="hidden" asp-for="Restaurant.Id"/>

<div>
<label asp-for="Restaurant.Name"></label>
<input asp-for="Restaurant.Name" />
<span asp-validation-for="Restaurant.Name"></span>
</div>

@* VSRD: Visual Studio has "Wrap with div" action but it's not reformatting the resulting markup, and as Extend Selection doesn't work in Razor, I have to select manually, then invoke Ctrl+K,F to reformat selection *@
<div>
<label asp-for="Restaurant.Cuisine"></label>
<select asp-for="Restaurant.Cuisine" asp-items="@Html.GetEnumSelectList<CuisineType>()"></select>
<span asp-validation-for="Restaurant.Cuisine"></span>
</div>

<input type="submit" name="save" value="Save" />
</form>
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using OdeToFoodVisualStudio.Models;
using OdeToFoodVisualStudio.Services;

namespace OdeToFoodVisualStudio.Pages.Restaurants
{
public class EditModel : PageModel
{
private IRestaurantData _restaurantData;

[BindProperty]
public Restaurant Restaurant { get; set; }

public EditModel(IRestaurantData restaurantData)
{
_restaurantData = restaurantData;
}

public IActionResult OnGet(int id)
{
Restaurant = _restaurantData.Get(id);
if (Restaurant == null)
{
return RedirectToAction("Index", "Home");
}
return Page();
}

public IActionResult OnPost()
{
if(ModelState.IsValid)
{
_restaurantData.Update(Restaurant);
return RedirectToAction("Details", "Home", new { id = Restaurant.Id });
}

return Page();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,6 @@ public interface IRestaurantData
IEnumerable<Restaurant> GetAll();
Restaurant Get(int id);
Restaurant Add(Restaurant restaurant);
Restaurant Update(Restaurant restaurant);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,10 @@ public Restaurant Add(Restaurant restaurant)
_restaurants.Add(restaurant);
return restaurant;
}

public Restaurant Update(Restaurant restaurant)
{
throw new NotImplementedException();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using OdeToFoodVisualStudio.Data;
using OdeToFoodVisualStudio.Models;

Expand Down Expand Up @@ -33,5 +34,13 @@ public IEnumerable<Restaurant> GetAll()
{
return _context.Restaurants.OrderBy(r => r.Name);
}

public Restaurant Update(Restaurant restaurant)
{
_context.Attach(restaurant).State = EntityState.Modified;
// VSRD: Visual Studio provides a kind of smart completion for non-imported EntityState after equals, nice. Inserts an FQN though :(
_context.SaveChanges();
return restaurant;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
<a href="/home/details/@restaurant.Id">Details</a>
@Html.ActionLink("Go", "Details", new { id = restaurant.Id })
<a asp-action="Details" asp-route-id="@restaurant.Id">More</a>
<a asp-page="/Restaurants/Edit" asp-route-id="@restaurant.Id">Edit</a>
</td>
</tr>
}
Expand Down

0 comments on commit d721c76

Please sign in to comment.