-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding the Edit form using Razor Pages
- Loading branch information
Showing
12 changed files
with
169 additions
and
0 deletions.
There are no files selected for viewing
25 changes: 25 additions & 0 deletions
25
OdeToFoodRider/OdeToFoodRider/Pages/Restaurants/Edit.cshtml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
42
OdeToFoodRider/OdeToFoodRider/Pages/Restaurants/Edit.cshtml.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
OdeToFoodVisualStudio/OdeToFoodVisualStudio/Pages/Restaurants/Edit.cshtml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
45 changes: 45 additions & 0 deletions
45
OdeToFoodVisualStudio/OdeToFoodVisualStudio/Pages/Restaurants/Edit.cshtml.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters