Test ASP. net mvc-Implementation of an invitation page, test mvc

Source: Internet
Author: User
Tags valid email address actionlink

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.