006.MVC Realization Registration Login Knowledge Point Summary

Source: Internet
Author: User

Summary of Knowledge points:

[DisplayName ("username")]//the name displayed, as opposed to the @Html of the View page. labelfor (x = x.username)
[Required (allowemptystrings=false,errormessage= "{0} cannot be empty")]//can verify non-null, if empty, returns the string after ErrorMessage
[Compare ("Password", errormessage= "two times password input inconsistent")]//this statement can be compared, often used to compare two times password input consistency when registering
[RegularExpression ("Regular expression code for mailbox here", errormessage= "{0} Mailbox format error, re-enter")]//can perform regular expression validation

@Html. validationmessagefor (x = X.username, "", new {style = "color:red"})
@* This statement returns the hint information specified on the model page, and can set the style *@

ModelState.IsValid:Model page All constraints are passed before the IF statement is executed, otherwise the Else statement is executed
if (modelstate.isvalid)
{}
Else
{}


Model page Code:

public class Register
{
[DisplayName ("username")]//the name displayed, as opposed to the @Html of the View page. labelfor (x = x.username)
[Required (allowemptystrings=false,errormessage= "{0} cannot be empty")]//can verify non-null, if empty, returns the string after ErrorMessage
public string Username {get; set;}
[DisplayName ("password")]
[Required (allowemptystrings = false, ErrorMessage = "{0} cannot be empty")]
public string Password {get; set;}
[DisplayName ("Confirm password")]
[Required (allowemptystrings = false, ErrorMessage = "{0} cannot be empty")]
[Compare ("Password", errormessage= "two times password input inconsistent")]//this statement can be compared, often used to compare two times password input consistency when registering
public string Ispwd {get; set;}
[DisplayName ("email address")]
[Required (allowemptystrings = false, ErrorMessage = "{0} cannot be empty")]
[RegularExpression ("Regular expression code for mailbox here", errormessage= "{0} Mailbox format error, re-enter")]//can perform regular expression validation
public string Email {get; set;}
[DisplayName ("role")]
[Required (allowemptystrings = false, ErrorMessage = "{0} cannot be empty")]
public string Role {get; set;}
}

View page code:

<body>
@using (Html.BeginForm ("register", "register"))
{
<div id= "D1" >
<div align= "center" style= "Background-color:gray" >
New User Registration </div>
<table border= "1" align= "Center" >
<tr>
<td>
@Html. labelfor (x = x.username) @* here corresponds to the [requiredname=] user name of the model page *@
</td>
<td>
@Html. textboxfor (x = x.username)
</td>
<td>
@* This statement returns the hint information specified on the model page, and can set the style *@
@Html. validationmessagefor (x = X.username, "", new {style = "color:red"})
</td>
</tr>
<tr>
<td>
@Html. labelfor (x = X.password)
</td>
<td>
@Html. passwordfor (x = X.password)
</td>
<td>
@Html. validationmessagefor (x = X.password, "", new {style = "color:red"})
</td>
</tr>
<tr>
<td>
@Html. labelfor (x = x.ispwd)
</td>
<td>
@Html. passwordfor (x = x.ispwd)
</td>
<td>
@Html. validationmessagefor (x = x.ispwd, "", new {style = "color:red"})
</td>
</tr>
<tr>
<td>
@Html. labelfor (x = x.email)
</td>
<td>
@Html. textboxfor (x = x.email) <br/>
</td>
<td>
@Html. validationmessagefor (x = X.email, "", new {style = "color:red"})
</td>
</tr>
<tr>
<td>
Role
</td>
<td> @Html. dropdownlistfor (x = x.role, new SelectList (new string[] {"Super admin", "Teacher", "Student"}))
</td>
<td>
@Html. validationmessagefor (x = X.role, "", new {style = "color:red"})
</td>
</tr>
</table>
<div align= "center" style= "Background-color:gray;" >
<input type= "Submit" value= "registration" style= "Background-color:green; width:100px "/>
</div>
</div>
}
</body>

Controller page Code:

public class Registercontroller:controller
{
//
GET:/register/

[httpget]//Initial display of the page
Public ActionResult Register ()
{
return View ();
}

[httppost]//to process the page
Public ActionResult Register (Day04 job practice registration and login. Models.register Register)
{
The non-null and password-consistent judgments specified by the Model page are all passed before the IF statement is executed, otherwise the Else statement is executed
if (modelstate.isvalid)
{
String sqlstr = String. Format ("INSERT into Register values (' {0} ', ' {1} ', ' {2} ', ' {3} ', ' {4} ')", Register. Username, register. Password,
Register. ISPWD, register. Email, register. Role);//command statement to invoke user-entered data with parameters within a method
int rows = 0;
Select the connection string, you need to set the configuration file first
String constring = configurationmanager.connectionstrings["STU"]. ConnectionString;

using (SqlConnection conn = new SqlConnection (constring))
{
SqlCommand cmd = new SqlCommand ();//declaring Command object
Cmd.commandtext = sqlstr;//Command (SQL) statement to execute
Cmd. Connection = conn;//Select the database connection to use for the command
Conn. Open ();//Opening connection
rows = cmd. ExecuteNonQuery ();//Returns the number of rows affected

}
if (Rows > 0)
{
Added success in page Popup registration successful dialog box
Return Content ("<script>alert (' registered successfully! ') <script> "," text/html ");
}

return View ();
}
Else
{
return View ();
}
}
}

006.MVC Realization Registration Login Knowledge Point Summary

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.