MVC implements a simple registration page

Source: Internet
Author: User

Just started to learn MVC, from the WebForm turned to really a little effort a lot of things have changed a form, but also constantly adapt to this, the following through a simple registration page to familiarize yourself with the common use of MVC.

The user class in model is as follows

<span style= "FONT-FAMILY:SIMSUN;FONT-SIZE:18PX;" >public class User    {        //Sign In name public        string LoginName {get; set;}        User Password public        string Password {get; set;}        User's email public        string Email {get; set;}        User's mobile number public        string phone {get; set;}    } </span>


The biggest benefit of MVC is the separation of concerns, meaning that we are not in the development process of what the page is, you can first develop business logic, the following is the operation of the Registercontroller

<span style= "FONT-FAMILY:SIMSUN;FONT-SIZE:18PX;" >public class Registercontroller:controller {//Registered information displays the public ActionResult Create (formcollect                Ion form) {User user = new User () {//two method of acquisition, one through request, one through the FormCollection class loginname=form["Name"],//LoginName = request.form["LoginName"], Password = R Equest.             form["Password"], phone = request.form["Phone"], email = request.form["email"};            Pass the value viewdata["UserInfo"] = user through ViewData to the interface;        return View ();  }//Registered method public ActionResult Reg () {//Dictionary type key-value pair Idictionary<int, string>            Star = new Dictionary<int, string> (); Add the value Star.            ADD (1, "Aries"); Star.            ADD (2, "Taurus"); Star.            ADD (3, "Gemini"); Star.            ADD (4, "cancer"); Star.            ADD (5, "Leo");Star.            ADD (6, "Virgo"); Star.            ADD (7, "Libra"); Star.            ADD (8, "Scorpio"); Star.            ADD (9, "Sagittarius"); Star.            ADD (10, "Capricorn"); Star.            ADD (11, "Aquarius"); Star.            ADD (12, "Pisces");            Assign a value to the list selectlist starlist = new SelectList (Star, "Key", "value");            Pass value to interface viewdata["star"] = Starlist;        return View (); }}</span>


With the above business logic we can implement the operation of the page.

Registration page

<span style= "FONT-FAMILY:SIMSUN;FONT-SIZE:18PX;" ><%@ page language= "C #" inherits= "system.web.mvc.viewpage<dynamic>"%><!    DOCTYPE html>

The detailed information page is as follows

<span style= "FONT-FAMILY:SIMSUN;FONT-SIZE:18PX;" ><%@ page language= "C #" inherits= "system.web.mvc.viewpage<dynamic>"%><! DOCTYPE html>


Case analysis

In the above example use a few key points, below to analyze.

1. Get pass-through values

In the above example we apply two methods to get the passed values, namely the FormCollection class and the Request object, both of which are used in the same way.

2. Page Jump

The beginform operation of the HtmlHelper class is used, such as Html.BeginForm ("Create", "Register", FormMethod.Post), which represents the way in which the page is submitted as a Post, Passed to the Create method in the Register controller. This method has many parameters and can be researched by itself.

3.viewdata to the page return value, ViewData is like a container, can load anything, the above example is passed by ViewData. For example, in the controller viewdata["UserInfo" = user, the user object is loaded into the container, and then through the page login.models.user user= viewdata["UserInfo"] as LoginMvc.Models.User to assign a value to the user on the page, be aware that a cast is required here.

The difference between 4.viewdata and TempData

ViewData property is a viewdatadictionary class, which can be used to store the data type of any object, but the stored key value must be a string ViewData There is a feature that will only exist in the current HTTP request, and not want to Session , you can take the data to the next HTTP request. with the ViewData , all belong to the dictionary class, but the data in him is only one page request .






MVC implements a simple registration page

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.