(1)Html.Encode
<%= Html.Encode(ViewData["Message"]) %>
定义和用法
HTMLEncode 方法对一段指定的字符串应用 HTML 编码。
语法
Server.HTMLEncode(
string
)参数 描述
string
必需。要编码的字符串。
实例
脚本:
<%
response.write(Server.HTMLEncode(
"
The image tag: <img>
"
))
%>
输出:
The image tag:
&
lt;img
&
gt;浏览器的输出:
The image tag:
<
img
>
(2)Html.RenderPartial
<% Html.RenderPartial("LoginUserControl"); %>
Html.RenderPartial(
"
viewName
"
);
我猜就是相当于一个include
把一个用户控件包含在当前面
"
viewName
"
当然就是一个用户控件了
但是这个用户控件貌似必须继承与System.Mvc.ViewUserControl这个类
而并非普通的用户控件
它的效果竟然和下面的代码一样
<
uc1:Header ID
=
"
viewName
"
runat
=
"
server
"
/>
(3)Html.ActionLink
<%= Html.ActionLink("Home", "Index", "Home")%>
<%=
Html.ActionLink(
string
linkText,
string
actionName,
string
controllerName)
%>
<%=
Html.ActionLink(
"
编辑
"
,
"
EditCategory
"
,
new
{ id
=
category.Id })
%>&
nbsp;
<%=
Html.ActionLink(
"
删除
"
,
"
DelCategory
"
,
new
{ id
=
category.Id },
new
{ onclick
=
"
return confirm('你确定要删除该随笔?')
"
})
%>
(4)<%= Html.TextBox("username") %>
<%= Html.Password("password") %>
<%= Html.CheckBox("rememberMe") %>
Code
<input id="username" name="username" type="text" value="" />
<input id="password" name="password" type="password" value="" />
<input id="rememberMe" name="rememberMe" type="checkbox" value="true" />
(5) Html.ValidationSummary()
<%= Html.ValidationSummary() %>
<%= Html.ValidationMessage("username") %>
例如,我们可以更新我们的视图,在文本框的右方使用Html.ValidationMessage()辅助方法,象这样

当页面因错而显示时,错误消息就会显示在有问题的控件域之后:

Html.ValidationMessage() 方法还有一个重载的版本,接受第二个参数,允许视图指定要显示的替换文字:

常见的一个用例是,在输入控件域后面输出 * 字符,然后将新的Html.ValidationSummary()辅助方法(也是第五个预览版中新加的)加到页面的顶部,来列出所有的错误消息:

Html.ValidationSummary()辅助方法然后就会用<ul><li></ul>显示出ModelState集合中的所有错误消息,以及 * 和 红边框会表示每个有问题的输入控件元素:

注意,我们完全不用改动ProductsController类就取得了如此效果。
(6)Html.BeginForm()
<%
using
(Html.BeginForm()) {
%>
<
div
>

..
</
div
>
<%
}
%>
Html.BeginForm 实例
使用Html.BeginForm来提交表单 收藏
以用户登录这个功能作为例子。
View中主要使用了Html.BeginForm(),
它在客户端产生一个<form action="/account/login" method="post"></form>标签。
具体代码如下:

<% using (Html.BeginForm())
{ %>
<ul id="login-form">
<li>
<div>电子邮件:</div>
<div><input name="email" type="text" /></div>
</li>
<li class="form-item">
<div>密 码:</div>
<div><input name="password" type="password" /></div>
</li>
</ul>
<div><input type="submit" value="登录" /></div>
<% } %>
除去<%%>中的内容,其他的html标签跟原始的html文件没什么两样,根本不使用传统的asp.net服务器端控件。
Controller中的login action 对应了相应的View.
要完成用户登录这个功能,首先要用Get的方法获取一个View,然后要用Post的方法接受提交的表单进行用户登录验证处理。
所以在Controller中会有两个Login action 但是这两个是不一样的,区别就在于GET和POST。
Get action 比较简单,如下:
//
// GET: /Account/Login/
public ActionResult Login()

{
return View();
}
POST action 比较复杂一些,要从Model模型中调用相应的功能,如下:

//
// POST: /Account/Login

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Login(FormCollection collection)

{
try

{
string email = collection["email"];
string password = collection["password"];
password = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(password, "md5");

XuShop.Models.common.membersInfo mi = new XuShop.Models.bll.members().Login(email, password);
if (mi != null)

{
HttpCookie c = new HttpCookie("member");
c.Value = XuShop.Models.web.Util.Member2String(mi);
c.Expires = DateTime.Now.AddDays(7);
Response.Cookies.Add(c);
System.Web.Security.FormsAuthentication.SetAuthCookie(mi.Email, false);

string url = Request.QueryString["ReturnUrl"];
if (!string.IsNullOrEmpty(url))
Response.Redirect(url);
return RedirectToAction("Index","Home");
}
else

{
ViewData["msg"] = "帐户或密码错误!";
return View();
}
}
catch(System.Exception ex)

{
ViewData["msg"] = ex.Message;
return View();
}
}

上面的 [AcceptVerbs(HttpVerbs.Post)]
指示该action使用POST。默认使用的是GET.