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

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

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 >

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

POST action 比较复杂一些,要从Model模型中调用相应的功能,如下:

//
        
// POST: /Account/Login

        [AcceptVerbs(HttpVerbs.Post)]
        
public ActionResult Login(FormCollection collection)
ExpandedBlockStart.gifContractedBlock.gif        
{
            
try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                
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)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
{
                    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
ExpandedSubBlockStart.gifContractedSubBlock.gif                
{
                    ViewData[
"msg"= "帐户或密码错误!";
                    
return View();
                }

            }

            
catch(System.Exception ex)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                ViewData[
"msg"= ex.Message;
                
return View();
            }

        }


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

 

转载于:https://www.cnblogs.com/NetDeng/archive/2009/11/04/1596073.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值