asp.net MVC < % = Html.XXX % > 解释

 

(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") %>


<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()辅助方法,象这样

ASP.NET MVC 第五个预览版和表单提交场景

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

ASP.NET MVC 第五个预览版和表单提交场景

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

ASP.NET MVC 第五个预览版和表单提交场景

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

ASP.NET MVC 第五个预览版和表单提交场景

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

ASP.NET MVC 第五个预览版和表单提交场景

  注意,我们完全不用改动ProductsController类就取得了如此效果。

 

(6)Html.BeginForm()

<% using (Html.BeginForm()) { %>

   
<div>..</div>

<% } %>
Html.BeginForm 实例

 
posted @ 2009-11-04 16:47  火星贝贝  阅读(2281)  评论(0编辑  收藏  举报