Exercises: Edit Model Classes
Add functionality to edit event objects in your CodingEvents application.
These exercises assume that you have added all of the code from this section of the book and your
application resembles the models branch
.
The edit form will resemble the form used to create an event.
As you work through these steps, test your code along the way! With each change you apply to your code, ask yourself what you expect to see when the application is run. You may not find that all of the steps result in observable changes, though. Use Visual Studio’s debugging tools and read your error messages if you run into issues after applying any of the changes.
Create the two action methods listed below in
EventsController. We’ll add code to these in a moment, so just put the outline in place for now.Create an action method to display an edit form with this signature:
1 2 3public IActionResult Edit(int eventId) { // controller code will go here }Create an action method to process the form with this signature:
1 2 3 4[HttpPost] public IActionResult SubmitEditEventForm(int eventId, string name, string description) { // controller code will go here }
Add the necessary annotations to the
SubmitEditEventForm()method for it to live at the path/Events/Edit.You’ll need to configure the route for
Edit()to include the path variableeventId, so that paths like/Events/Edit/3will work.Create an
Edit.cshtmlview inViews/Events.Copy the code from
Add.cshtmlintoEdit.cshtml.- You’ll want to update the text of the submit button and the heading to reflect the edit functionality.
Back in
EventsController, round out theEdit()method.Use an
EventDatamethod to find the event object with the giveneventId.Put the event object in
ViewBag.Return the appropriate view.
Within the form fields in
Edit.cshtml,Get the name and description from the event that was passed in via
ViewBagand set them as the values of the form fields.Add
action="/events/edit"to theformtag.
Add another input to hold the id of the event being edited. This should be hidden from the user:
<input type="hidden" value="@ViewBag.eventToEdit.Id" name="eventId" />NoteYou may not have named your
ViewBagpropertyeventToEdit. Make sure you are using the name you gave your property!Back in the
Edit()action method, add a title toViewBagthat reads“Edit Event NAME (id=ID)"where"NAME"and"ID"are replaced by the values for the given event.In
SubmitEditEventForm(),Query
EventDatafor 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).
To access event editing, the user will need an edit option in the list of event data.
In
Index.cshtml, add a link to edit the event as a column in the event table:<td><a asp-controller="Events" asp-action="Edit" asp-route-id="@evt.Id">Edit Event</a></td>asp-route-idis a new tag helper for us. Our routes normally go/<controller>/<action>.asp-route-idpasses an{id?}parameter at the end of our route. When the site is built, we can inspect it and see that for the first item in the table this line of HTML will look like:<td><a href="/Events/Edit/1">Edit Event</a></td>