See a blogger's article feel very useful, take over to do it yourself:
In the MVC framework, there are some default routing rules (which can be customized, of course), and this rule is more specific
All right, let's get an example straight!
First, create an empty MVC project, this is MVC version 2.0.
This is named "Firstmvc", which automatically adds the project's directory structure.
Create a new controller under the "controller" directory named "MemberController.cs"
At the same time, add the corresponding view under the "View" directory, and add an index
The operation of the data logic is basically done in the controller, which is implemented in MemberController.cs.
To be more realistic, we also build an entity class under the model directory named Member.cs
All right, get ready for the job, let's do it.
Front desk--view
<%@ page language= "C #" inherits= "system.web.mvc.viewpage<dynamic>"%>
<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<style type= "Text/css" >
Table
{
width:50%;
border-top:1px solid #e5eff8;
border-right:1px solid #e5eff8;
Margin:1em Auto;
Border-collapse:collapse;
}
Td
{
Color: #678197;
border-bottom:1px solid #e5eff8;
border-left:1px solid #e5eff8;
padding:. 3em 1em;
Text-align:center;
}
</style>
<script src= "Http://www.cnblogs.com/Scripts/jquery-1.4.1.min.js" type= "Text/javascript" ></script>
<title>Index</title>
<script language= "javascript" type= "Text/javascript" >
$ (document). Ready (function () {
$ ("#Button1"). Click (function () {
var msg = "Hello, user" + $ ("#loginName"). Val () + ", \ n"
+ "Please remember your password:" + $ ("#passWord"). Val () + ", \ n"
+ "Your user number is:" + $ ("#namelist"). Val ();
Alert (msg);
})
})
</script>
<body>
<div>
Refine the information below <table id= "Memberarea" >
<thead>
<tr>
<TD colspan= "2" >
User Registration </td>
</tr>
</thead>
<tbody>
<tr>
<td>
Login Name
</td>
<td>
<%=html.textbox ("LoginName")%>
</td>
</tr>
<tr>
<td>
User name
</td>
<td>
<%=html.textbox ("UserName")%>
</td>
</tr>
<tr>
<td>
Password
</td>
<td>
<%=html.password ("Password")%>
</td>
</tr>
<tr>
<td>
real name
</td>
<td>
<%=html.dropdownlist ("NameList")%>
</td>
</tr>
<tr>
<TD colspan= "2" align= "Center" >
<br/>
<input type= "button" id= "Button1" value= "OK"/>
</td>
</tr>
</tbody>
</table>
</div>
</body>
Backstage--controller
Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Web;
Using SYSTEM.WEB.MVC;
Using System.Data;
Using System.Data.SqlClient;
Using System.Configuration;
Using firstmvc.models;//Note adding references
Namespace Firstmvc.controllers
{
<summary>
The first instance of the MVC schema
Recent exposure to MVC, see most tutorials Take
Linqtosql, here's an ADO way of writing
Author:luckyhu
Date:2012-03-30
</summary>
public class Membercontroller:controller
{
//
GET:/menber/
Define some global variables
String connstr = configurationmanager.connectionstrings["ConnectionString"]. ConnectionString;
SqlConnection conn = null;
SqlCommand cmd = null;
SqlDataReader SDR = null;
Public ActionResult Index ()
{
Models.member Member = new Models.member ();
SDR = GetData ();
int uid = 0;
String name = "";
Idictionary<int, string> namelist = new Dictionary<int, string> ();
IF (Conn. state = = connectionstate.closed)
{
Conn. Open ();
}
Try
{
while (SDR. Read ())
{
UID = Int. Parse (sdr["UID"). ToString ());
Name = sdr["Name"]. ToString ();
Namelist.add (uid, name);
}
}
catch (SqlException ex)
{
Response.Write ("<script> var ex =" + ex.) ToString () + "; Alert (ex);</script> ");
}
Finally
{
IF (Conn. state = = ConnectionState.Open)
{
Conn. Close ();
}
}
SelectList selectnamelist = new SelectList (NameList, "key", "value");//Construct a drop-down list of key-value pairs
viewdata["NameList"] = selectnamelist;
return View ();
}
<summary>
Constructing test data
</summary>
<returns></returns>
Public SqlDataReader GetData ()
{
conn = new SqlConnection (CONNSTR);
String sql = "SELECT * from users";
IF (Conn. state = = connectionstate.closed)
{
Conn. Open ();
}
Try
{
cmd = new SqlCommand (SQL, conn);
SDR = cmd. ExecuteReader ();
}
catch (SqlException ex)
{
Response.Write ("<script> var ex =" + ex.) ToString () + "; Alert (ex);</script> ");
}
return SDR;
}
}
}
Solid Layer--model
View CodeUsing System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Web;
Namespace Firstmvc.models
{
public class Member
{
Fields
private int uid_ = 0;
private string name_ = "";
private int jib_ = 0;
private string Email_ = "";
private string qq_ = "";
Private DateTime Rdage_;
Private DateTime Ndate_;
Properties
public int Uid_
{
get {return uid_;}
set {uid_ = value;}
}
public string name_
{
get {return name_;}
set {name_ = value;}
}
public int Jib_
{
get {return jib_;}
set {jib_ = value;}
}
Public DateTime Rdage_
{
get {return rdage_;}
set {Rdage_ = value;}
}
public string Email_
{
get {return email_;}
set {Email_ = value;}
}
public string Qq_
{
get {return qq_;}
set {qq_ = value;}
}
Public DateTime Ndate_
{
get {return ndate_;}
set {Ndate_ = value;}
}
Construcution
Public Member ()
{
}
Function
Public Member (int uid, string name, int jib, string email, string qq, DateTime rdage, datetime ndate)
{
This.uid_ = UID;
This.jib_ = jib;
This.email_ = email;
This.qq_ = QQ;
This.rdage_ = Rdage;
This.ndate_ = ndate;
}
}
}
Here the database is accessed using the traditional ADO, a lot of tutorials used by Linqtosql.
There are differences between the MVC framework and the Webfrom, which may not be well understood at first, but there are plenty of common areas between them.
At this point, let's not forget to configure the route, change it in the Global.asax file,
Effect
A simple. NET MVC Instance