Exercises Solutions: Edit Model Classes¶
Line numbers are for reference. They may not match your code exactly.
Add the necessary annotations to the
SubmitEditEventForm()
method for it to live at the path/Events/Edit
. See Line 2.1 2 3 4 5 6
[HttpPost] [Route("Events/Edit")] public IActionResult SubmitEditEventForm(int eventId, string name, string description) { // controller code will go here }
You’ll need to configure the route for
Edit()
to include the path variableeventId
, so that paths like/Events/Edit/3
will work. See Line 2.1 2 3 4 5 6
[HttpPost] [Route("Events/Edit/{eventId}")] public IActionResult Edit(int eventId) { // controller code will go here }
Copy the code from
Add.cshtml
intoEdit.cshtml
.You’ll want to update the text of the submit button and the heading to reflect the edit functionality.
1 2 3 4 5 6 7 8 9 10 11 12 13
<h1>Edit Event</h1> <form method="post"> <div class="form-group"> <label for="name">Name</label> <input name="name" type="text" /> </div> <div class="form-group"> <label for="description">Description</label> <input name="description" /> </div> <input type="submit" value="Edit Event" /> </form>
Back in
EventsController
, round out theEdit()
method.1 2 3 4 5 6 7 8
[HttpGet] [Route("Events/Edit/{eventId}")] public IActionResult Edit(int eventId) { Event editingEvent = EventData.GetById(eventId); ViewBag.eventToEdit = editingEvent; return View(); }
Within the form fields in
Edit.cshtml
,Get the name and description from the event that was passed in via
ViewBag
and set them as the values of the form fields.Add
action="/events/edit"
to the form tag.
1 2 3 4 5 6 7 8 9 10 11 12 13 14
<h1>@ViewBag.title</h1> <form method="post" action="/events/edit"> <div class="form-group"> <label for="name">Name</label> <input name="name" type="text" value="@ViewBag.eventToEdit.Name"/> </div> <div class="form-group"> <label for="description">Description</label> <input name="description" type="text" value="@ViewBag.eventToEdit.Description" /> </div> <input type="submit" value="Edit Event" /> </form>
Add another input to hold the id of the event being edited. This should be hidden from the user:
10 11 12 13 14
<!-- description div code here --> </div> <input type="hidden" value="@ViewBag.eventToEdit.Id" name="eventId"> <input type="submit" value="Edit Event" /> </form>
Back in the
Edit()
action method, add a title toViewBag
that reads“Edit Event NAME (id=ID)”
where"NAME"
and"ID"
are replaced by the values for the given event.1 2 3 4 5 6 7 8 9
[HttpGet] [Route("Events/Edit/{eventId}")] public IActionResult Edit(int eventId) { Event editingEvent = EventData.GetById(eventId); ViewBag.eventToEdit = editingEvent; ViewBag.title = "Edit Event " + editingEvent.Name + "(id = " + editingEvent.Id + ")"; return View(); }
In
SubmitEditEventForm()
,
Query
EventData
for the event being edited with the given id parameter.Update the name and description of the event.
Redirect the user to
/Events
(the event listing page).1 2 3 4 5 6 7 8 9
[HttpPost] [Route("Events/Edit")] public IActionResult SubmitEditEventForm(int eventId, string name, string description) { Event editingEvent = EventData.GetById(eventId); editingEvent.Name = name; editingEvent.Description = description; return Redirect("/Events"); }
In
Index.cshtml
, add a link to edit the event as a column in the event table. See Line 38.
32 33 34 35 36 37 38 39 40 41 @foreach (var evt in ViewBag.events) { <tr> <td>@evt.Id</td> <td>@evt.Name</td> <td>@evt.Description</td> <td><a asp-controller="Events" asp-action="Edit" asp-route-id="@evt.Id">Edit Event</a></td> </tr> } </table>