书接上文,昨天我们写了一个简单的注册,今天我们来改写一下,改成像是MVC框架的样子,或者说本来就应该这么写。
先来看看UserController里面,我们在昨天写了两个属性,是用来从表单中取值的:
![](http://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
Code
1
private string UserName
2![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](http://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
3
get
4![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
5
return this.Request.Form["txtName"];
6
}
7
}
8
private string UserPwd
9![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](http://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
10
get
11![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
12
return this.Request.Form["txtPwd"];
13
}
14
}
15![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
如果是以前写WebForm写习惯了的朋友,这么写其实很常见,或者说我们大部分都是这么写的,现在在MVC中,我们从表单中获取数据就更加方便了,直接在Action中添加两个参数就OK了:
![](http://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
Code
1
public ActionResult Register(string txtName,string txtPwd)
2![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](http://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
3
if (us.CheckInput(txtName,txtPwd))
4![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
5
if (us.IsExist(txtName))
6![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
7
UserInfo user = new UserInfo
8![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
9
UserName=txtName,
10
UserPwd=txtPwd
11
};
12
us.InsertUser(user);
13
ViewData["Message"] = "添加成功!";
14![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
15
}
16
else
17![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
18
ViewData["Message"] = "用户已存在";
19
}
20
}
21
return View();
22
}
这里你要注意的是Action后面的参数必须要和你表单元素的名字一致,不然是获取不到的。但是MCV框架提供了一种更简便的方式,我们来看下:
![](http://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
Code
1
public ActionResult Register(UserInfo user)
2![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](http://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
3
if (us.CheckInput(user.UserName, user.UserPwd))
4![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
5
if (us.IsExist(user.UserName))
6![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
7
us.InsertUser(user);
8
ViewData["Message"] = "添加成功!";
9
}
10
else
11![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
12
ViewData["Message"] = "用户已存在";
13
}
14
}
15
return View();
16
}
这种写法也是有要求的,那就是你表单中元素的名称,必须要和你绑定的对象中的属性一致,比如说这个注册,如果我要这么写的话,表单中元素的命名必须为UserName和UserPwd,这样才能获取到绑定的对象。
现 在Action貌似是改好了,可是现在问题又来了,当你点击首页的注册链接的时候,就会报错,因为首页的连接是没有传递任何参数进来的。解决这个问题的办 法当然也不是唯一的,你可以新写一个无参的Action,然后返回View("Register")。下面我要说的是另外一种方法,我在 UserController中新添加一个Action:
1
public ActionResult Register()
2![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](http://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
3
return View();
4
}
这么一看,貌似是重载了Action,但其实运行的时候会报错,这也很好理解,因为在Routing规则中是提交到了Register 中,但是从名字上来说是一模一样的,使得指代不明确。那么我们如何解决这个问题,其实也很简单,只需要在这个处理表单的Action上加一个特性就好了:
![](http://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
Code
1
[AcceptVerbs(HttpVerbs.Post)]
2
public ActionResult Register(UserInfo user)
3![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](http://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
4
if (us.CheckInput(user.UserName, user.UserPwd))
5![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
6
if (us.IsExist(user.UserName))
7![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
8
us.InsertUser(user);
9
ViewData["Message"] = "添加成功!";
10
}
11
else
12![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
13
ViewData["Message"] = "用户已存在";
14
}
15
}
16
return View();
17
}
这个特性的意思是说,只有当处理Post请求的时候才会调用它,很显然首页的超链接是个Get请求,这样我们就把它们区分开来了。
再来说View。这是我们昨天写的View:
1![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
<%
Html.BeginForm("Register", "User"); %>
2
用户名:<%=Html.TextBox("txtName") %><br />
3
密 码:<%=Html.Password("txtPwd") %><br />
4
<input type="submit" value="提交" />
5![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
<%
Html.EndForm(); %>
6![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
在实际开发中,页面实际上时美工做好了的,对于美工来说,表单中的元素都是像这样的<input type="text" />,难到说我们拿到页面之后第一件事就是把所有元素拿来改成咱们昨天写的那个样子吗?相信这是个不小的工作量,那么我们就干脆换成Html标签的 样子好了:
![](http://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
Code
1
<form action="Register" method="post">
2
用户名:<input type="text" name="UserName" id="txtName" /><br />
3
密 码:<input type="password" name="UserPwd" id="txtPwd" /><br />
4
<input type="submit" value="提交" />
5
</form>
运行一下也没什么问题,但是这么写有一个缺陷,万一哪天Routing规则变了,你这里还要改Action,为了一劳永逸,我们就干脆这么写:
![](http://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
Code
1![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
<%
Html.BeginForm("Register", "User"); %>
2
用户名:<input type="text" name="UserName" id="txtName" /><br />
3
密 码:<input type="password" name="UserPwd" id="txtPwd" /><br />
4
<input type="submit" value="提交" />
5![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
<%
Html.EndForm(); %>
这样写的好处是,对于Routing的修改没有影响,然后不影响美工的开发,自己也不用多干好多事儿了。那么如果你一定要用昨天写的方式来做,可能会设计到元素样式的问题,我们大概写一下:
1
<%=Html.TextBox("txtName","请输入用户名",new{style="border-width:1px;border-style:solid"})%>
这样就写成了一个细边框样式的文本框,如果要用类选择器来写,可以这样:
1
<%=Html.TextBox("txtName","请输入用户名",new{@class="mystyle"})%>
今天就改到这里吧~谢谢大家捧场:)
代码在这里