⼀个简单的MVC实例
久闻MVC的⼤名,只是⼀直没有去研究过。过去⾃⼰⽤的最多的还是 WebForm,⽐较习惯三层架构。由于最近公司将会使⽤到MVC框架做⼀些项⽬,所以⾃⼰也开始接触和学习。看了⼀些教程,⼤体搞清楚了M-V-C的⼤致意思,⼤致了解了下这个架构,但不得不承认有些具体的细节地⽅和WebFrm还是有差别的,这⾥只是简单做⼀个实例,让它跑起来。看了⼀些实例,多数都是⽤的是LinqToSql,这⾥⽤的是ADO的。感觉这样更顺⼿些,呵呵。
西施打一字由于之前做过PHP程序,所以对于前台页⾯的展⽰⽅式还是容易理解的。只是MVC框架⾥,有些默认的路由规则(当然是可以⾃定义的),这个规则⽐较特别
好了,直接来个实例吧!
⾸先,新建⼀个空的MVC项⽬,如图,这⾥是MVC 2.0版
这⾥命名为"FirstMvc" ,⾃动添加好了项⽬的⽬录结构
在"Controller"⽬录下⾯新建⼀个控制器,命名为"MemberController.cs"
同时在"View"⽬录下添加相应的视图,这⾥添加⼀个Index
数据逻辑的操作基本上是在控制器⾥⾯完成的,这⾥就是在MemberController.cs实现。
为了更加真实,我们也在Model⽬录下⾯构建⼀个实体类,命名为Member.cs
好了,准备⼯作做好,就开始吧
前台——View
<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<dynamic>" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "www.w3/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="www.w3/1999/xhtml">
<head runat="rver">
<style type="text/css">
table
{
width: 50%;
border-top: 1px solid #e5eff8;
border-right: 1px solid #e5eff8;
margin: 1em auto;
border-collap: collap;
}
td
{
color: #678197;
border-bottom: 1px solid #e5eff8;
border-left: 1px solid #e5eff8;
padding: .3em 1em;
text-align: center;
}
</style>
<script src="/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 = "您好,⽤户" + $("#loginName").val() + ",\n"
+ "请记好您的密码:" + $("#passWord").val() + ",\n"
+ "你的⽤户编号为:" + $("#namelist").val();
alert(msg);
})
})
</script>
</head>
<body>
<div>
<h2>
完善下⾯的信息</h2>
<table id="MemberArea">自驾游注意事项
<thead>
油泼鱿鱼
<tr>
<td colspan="2">
<h3>
⽤户注册</h3>
</td>
</tr>
</thead>
<tbody>
<tr>
<td>
登录名
</td>
<td>
<%=Html.TextBox("loginName")%>
</td>
</tr>
<tr>
<td>
⽤户名
</td>
<td>
<%=Html.TextBox("urName")%>
鱼图片简笔画
</td>
</tr>
<tr>
<td>
密码
鸡怎么做最好吃</td>
<td>
<%=Html.Password("passWord")%>
</td>
</tr>
<tr>
<td>
真实姓名
</td>
<td>
<%=Html.DropDownList("namelist")%>
</td>
</tr>
<tr>
<td colspan="2" align="center">
<br />
<input type="button" id="Button1" value="确定"/>
</td>
</tr>
</tbody>
</table>
</div>
</body>
</html>
后台——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;//注意添加引⽤
namespace FirstMvc.Controllers
{
///<summary>
///第⼀个MVC架构实例
///最近接触MVC,看到⼤多数教程采⽤
/// LinqToSql,这⾥写⼀个ADO⽅式的
/// Author:LuckyHu
/// Date:2012-03-30
/
//</summary>
public class MemberController : Controller
{
//
// GET: /Menber/
//定义⼀些全局变量
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.Clod)
{
conn.Open();
}
try
{
while (sdr.Read())
{
uid = int.Par(sdr["uid"].ToString());
name = sdr["name"].ToString();
nameList.Add(uid, name);
}
}
catch (SqlException ex)
{
Respon.Write("<script> var ex = " + ex.ToString() + "; alert(ex);</script>");
}
finally
青岛名吃{
if (conn.State == ConnectionState.Open)
{
conn.Clo();
}
}
SelectList lectNameList = new SelectList(nameList, "Key", "Value");//构造⼀个下拉列表的键值对 ViewData["namelist"] = lectNameList;
return View();
}
孕妇学校
///<summary>
///构造测试数据
///</summary>
///<returns></returns>
public SqlDataReader GetData()
{
conn = new SqlConnection(connStr);
string sql = "lect * from urs";
if (conn.State == ConnectionState.Clod)
{
conn.Open();
}
try
{
cmd = new SqlCommand(sql, conn);
sdr = cmd.ExecuteReader();
}
catch (SqlException ex)
{
Respon.Write("<script> var ex = " + ex.ToString() + "; alert(ex);</script>"); }
return sdr;
}
}
}
实体层——Model
View Code
using 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_; }
t { uid_ = value; }
}
public string Name_
{
get { return name_; }
t { name_ = value; }
}
public int Jib_
{
get { return jib_; }
t { jib_ = value; }
}
public DateTime Rdage_
教室作文{
get { return rdage_; }
t { rdage_ = value; }
}
public string Email_
{
get { return email_; }
t { email_ = value; }
}
public string Qq_
{
get { return qq_; }
t { qq_ = value; }
}
public DateTime Ndate_
{
get { return ndate_; }
t { 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.qq_ = qq;
this.rdage_ = rdage;
this.ndate_ = ndate;
}
}
}
这⾥数据库访问使⽤的传统的ADO的,很多教程使⽤的LinqToSql的。
MVC框架和WebFrom之间有差异,可能刚开始不是很好理解,但是它们之间确实也有很多公⽤的地⽅。这个时候,我们别忘了配置路由,在Global.asax⽂件中更改,
效果