Home > Web Front-end > HTML Tutorial > MVC implements simple registration page_html/css_WEB-ITnose

MVC implements simple registration page_html/css_WEB-ITnose

WBOY
Release: 2016-06-24 11:53:00
Original
1710 people have browsed it

I just started learning MVC, and it was a bit difficult to switch from Webform. Many things have already changed to another form, and I am constantly adapting to this. Let’s explain a simple registration page. Let’s get familiar with the commonly used operations in MVC.

The User class in Model is as follows

<span style="font-family:SimSun;font-size:18px;">public class User    {        //用号登录姓名        public string LoginName { get; set; }        //用户密码        public string Password { get; set; }        //用户的电子邮件        public string Email { get; set; }        //用户的手机号码        public string Phone { get; set; }    }</span>
Copy after login


The biggest benefit of MVC is separation of concerns, which means we are developing In the process, you don’t need to worry about what the page is. You can develop business logic first. The following are the operations in RegisterController

<span style="font-family:SimSun;font-size:18px;">public class registerController : Controller    {             //注册完的信息显示        public ActionResult Create(FormCollection form)        {            User user = new User()            {                //两种获取方法,一种通过request,一种通过formcollection类                LoginName=form["name"],               // LoginName = Request.Form["loginName"],                Password = Request.Form["password"],                Phone = Request.Form["phone"],                Email = Request.Form["email"]            };            //通过viewdata向界面传递值            ViewData["UserInfo"] = user;            return View();        }        //注册的方法        public ActionResult Reg()        {            //字典类型的键值对            IDictionary<int, string> star = new Dictionary<int, string>();            //添加值            star.Add(1, "白羊座");            star.Add(2, "金牛座");            star.Add(3, "双子座");            star.Add(4, "巨蟹座");            star.Add(5, "狮子座");            star.Add(6, "处女座");            star.Add(7, "天秤座");            star.Add(8, "天蝎座");            star.Add(9, "射手座");            star.Add(10, "摩羯座");            star.Add(11, "水瓶座");            star.Add(12, "双鱼座");            //为列表赋值            SelectList starList = new SelectList(star, "key", "value");            //向界面传递值            ViewData["star"] = starList;            return View();        }    }</span>
Copy after login


With the above business logic, we can implement it The page is operated.

Registration page

<span style="font-family:SimSun;font-size:18px;"><%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<dynamic>" %><!DOCTYPE html><html><head runat="server">    <meta name="viewport" content="width=device-width" />    <title>Reg</title></head><body>  <div id="register"><% using (Html.BeginForm("Create", "Register", FormMethod.Post))   { %>    <table border="0" width="500">    <thead><tr><td colspan="2">        <h3>用户注册</h3>    </td></tr></thead><tbody>    <tr><td class="td_left">登录名:</td>        <td><%=Html.TextBox("loginName") %></td>    </tr><tr><td class="td_left">密码:</td>        <td><%=Html.Password("password") %></td>    </tr><tr><td class="td_left">确认密码:</td>        <td><%=Html.Password("password2") %></td>    </tr><tr><td class="td_left">星座:</td>        <td><%=Html.DropDownList("star") %></td>    </tr><tr><td class="td_left">性别:</td>        <td><%=Html.RadioButton("sex", true, new { style = "border:0; width:30px;" })%>男            <%=Html.RadioButton("sex", false, new { style = "border:0; width:30px;" })%>女</td>    </tr><tr><td class="td_left">已婚:</td>        <td><%=Html.CheckBox("married", false, new { style = "border:0; width:30px;" })%></td>    </tr><tr><td class="td_left">安全邮箱:</td>        <td><%=Html.TextBox("email") %>输入邮箱</td>    </tr><tr><td class="td_left">联系电话:</td>        <td><%=Html.TextBox("phone") %>输入联系电话</td>    </tr><tr>    <td colspan="2" align="center"><br /><button type="submit"> 提交 </button><br /><br /></td>    </tr>    </tbody>    </table><%} %></div></body></html></span>
Copy after login

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><html><head runat="server">    <meta name="viewport" content="width=device-width" />    <title>Create</title></head><body>   <div id="register">    <h2>用户注册</h2>    <% loginMvc.Models.User user =            ViewData["UserInfo"] as loginMvc.Models.User; %>    你刚才提交的信息如下:<br />    登 录 名:<%= user.LoginName %><br />    登录密码:<%= "******" %><br />    安全邮箱:<%= user.Email %><br />    联系电话:<%= user.Phone %><br />    <br />    <button>确定注册</button>    <a href="/Register">返回重填</a>    <br /><br /></div></body></html></span>
Copy after login


Case Analysis

Several key points are used in the above example. Let’s analyze it below.

1. Get the passed value

In the above example, we use two methods to get the passed value, namely the FormCollection class and the Request object. These two usages are the same.

2. Page jump

The BeginForm operation of the HtmlHelper class is used, such as Html.BeginForm("Create", "Register", FormMethod.Post), which represents the post method as the page. When submitting, pass it to the Create method in the Register controller. There are many parameters of this method, you can study them yourself.

3. Viewdata returns a value to the page. Viewdata is like a container that can load anything. In the above example, it is passed through viewdata. For example, in the controller ViewData["UserInfo"] = user; loads the user object into the container, and then uses login.models.user user= ViewData["UserInfo"] as loginMvc.Models.User on the page to user assignment, it should be noted that forced conversion is required here.

4. The difference between viewdata and TempData

The ViewData property is a ViewDataDictionary class that can be used to store the data type of any object, but the stored key value must be a string. ViewData has a feature, which is It will only exist in the current HTTP request, and unlike the session, the data can be carried to the next HTTP request. Like ViewData, both belong to the dictionary class, but the data in it is just a request for a web page




??

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template