ASP.NET Core - 2.pdf - Storing Responses u2022 Now that we...

This preview shows page 1 - 8 out of 34 pages.

Storing Responses Now that we have some mechanism to store the data. Its time to update our action method again so it will receive the HTTP Post request to store them [HttpPost] public ViewResult RsvpForm(GuestResponse guestResponse) { Repository.AddResponse(guestResponse); return View("Thanks", guestResponse); }
Why Model Binding is Not Like Web Forms One of the disadvantages of traditional ASP.NET Web Forms is that it hides the details of HTTP and HTML from the developers. But does the MVC model binding that has been used to create a GuestResponse object from an HTTP POST is doing the same thing? Model binding saves a lot of error-prone task of having to inspect an HTTP request and extract all the data values that we require. But MVC provides easy access to all of the request data. Nothing is hidden from the developer, but there are a number of useful features that make working with HTTP and HTML simpler and easier.
The View method invocation in the RsvpForm action method tells MVC to render a view called Thanks Which passes the GuestResponse object to the view. To create the view, right-click the Views/Home folder in the Solution Explorer. Select Add View from the pop-up menu. Set the name to Thanks.cshtml , and click the Add button.
@model PartyInvites.Models.GuestResponse @{ Layout = null;} <!DOCTYPE html> <html> <head> <meta name ="viewport" content="width=device-width" /> <title>Thank you</title> </head> <body> <p> <h1>Thank you, @Model.Name!</h1> @if(Model.WillAttend == true) { @:Its great that you are coming, more information about the party is coming. } else { @:Sorry to know that you can't make it, Thankyou for letting us know } </p> <p>Click < a asp-action ="ListResponses">here</ a > to see who else is coming</p> </body> </html>
Thank you.cshtml view uses the Razor to display content based on the value of the GuestResponse properties that we passed to the View method in the RsvpForm action method. The Razor @model expression specifies the domain model type with which the view is strongly typed. To access the value of a property in the domain object, we use Model.PropertyName. For example, to get the value of the Name property, we call Model.Name.
Displaying the Responses Now let’s create the action method against the ListResponse asp-action we added. using System.Linq; public ViewResult ListResponses() { return View(Repository.Responses.Where(r => r.WillAttend == true)); } The new action method is called ListResponses , and it calls the View method, using the Repository. Responses property as the argument. This is how an action method provides data to a strongly typed view. The collection of GuestResponse objects is filtered using LINQ so that only positive responses are used.
The ListResponses action method doesn’t specify the name of the view that should be used to display the collection of GuestResponse objects.

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture