Test ASP. net mvc-Implementation of an invitation page, test mvc
In the previous blog, we introduced ASP. net mvc and how to create a project. Try ASP. net mvc. Let's write a simple WEB invitation.
Create a Models class named GuestResponse and write the following code.
public class GuestResponse { [Required(ErrorMessage = "Please enter your name")] public string Name { get; set; } [Required(ErrorMessage = "Please enter your email address")] [RegularExpression(".+\\@.+\\..+",ErrorMessage = "Please enter a valid email address")] public string Email { get; set; } [Required(ErrorMessage = "Please enter your phone number")] public string Phone { get; set; } [Required(ErrorMessage = "Please specify whether you'll attend")] public bool? WillAttend { get; set; } }
Next, naturally, it is the homepage. Let's display a greeting and invite the visitor's text.
Create the HomeController. cs file in the Controller and write the following code in its Index method.
public ViewResult Index() { int hour = DateTime.Now.Hour; ViewBag.Greeting = hour < 12 ? "Good Morning" : "Good Afternoon"; return View(); }
Next, the Index page is rendered. Create an Index View File and fill in the following code. (The bootstrap framework is used in the code, but I will not explain it here. The children's shoes You Want To Know use the search engine on their own)
Html. ActionLink is an Html auxiliary method. Its first parameter is the text displayed for the link, and the second parameter is the action Method for clicking the link jump.
Next, run the project and you will see the following interface.
However, a website certainly does not only have an invitation information page. Next we need to jump to another page. Write an RsvpForm method in HomeController and render the RsvpForm view.
public ViewResult RsvpForm() { return View(); }
Then write the RsvpForm view.
Run the project and we will find that when you click the PSVP Now button, it will jump to this interface.
This interface is used to submit the information of the invitee. At this time, we will find a problem. We didn't tell MVC what to do when the form is submitted to the server. When you click Submit RSVP, the form will be handed back to the RsvpForm method in the Home controller, which only renders the view again. Here we need the [HttpGet] and [HttpPost] annotations. Get is to get data from the server (apparently in this project it is the page), post is to send data to the server (here the data is the content of the form ). We write an RsvpForm overload method to process form submission. The change code is as follows:
[HttpGet] public ViewResult RsvpForm() { return View(); } [HttpPost] public ViewResult RsvpForm(GuestResponse guestResponse) { if (ModelState.IsValid) { return View("Thanks", guestResponse); } else { return View(); } }
After submitting the form through Post, we certainly need to display a thank-you page to show that it is friendly. Create the Thanks view and fill in the following code.
So far, this project has been initially completed.