惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Vercel News
Vercel News
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Apple Machine Learning Research
Apple Machine Learning Research
T
Tailwind CSS Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
人人都是产品经理
人人都是产品经理
V
V2EX
量子位
Last Week in AI
Last Week in AI
Jina AI
Jina AI
博客园 - 【当耐特】
爱范儿
爱范儿
宝玉的分享
宝玉的分享
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Hugging Face - Blog
Hugging Face - Blog
博客园 - 三生石上(FineUI控件)
有赞技术团队
有赞技术团队
小众软件
小众软件
IT之家
IT之家
博客园_首页
博客园 - 聂微东
S
SegmentFault 最新的问题
阮一峰的网络日志
阮一峰的网络日志
博客园 - 叶小钗

博客园 - lu xu

OWIN and Katana [转]我的微软开源CMS-Orchard源码阅读之Orchard Data Migrations 学习javascript必须订阅30个程序员的Blog 我的微软开源CMS-Orchard源码阅读之Orchard.Caching [转]jquery跨域调用asp.net web service 经纬度坐标系与UTM MGRS坐标系之间的转换 c# 版本 因为我们是软件公司 世界上最幸福的职业-鉴黄师 纪念一下 不能免俗-bing的试用 只看不说-CCTV的客户端关键字 asp.net mvc 点滴 一 asp.net mvc vs asp.net web forms Oracle收购Sun [转载]一个普通程序如何摧毁了华尔街 asp.net mvc 1.0 开源 免费 官方的ASP.NET MVC电子书-Professional ASP.NET MVC 1.0 Asp.Net MVC 学习心得 之 Model Asp.Net MVC 学习心得 之 View Asp.Net MVC 学习心得 之 Controllers
Asp.Net MVC 学习心得 之 Html Helper
lu xu · 2009-03-09 · via 博客园 - lu xu

首先使用Asp.Net MVC可以不使用Html Helper,不过使用了Html Helper可以节约很多时间的O(∩_∩)O~

一、标准Html Helper

.ActionLink

创建一个链接,但现在还不能创建一个带图片的链接

<%@ Page Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage" %>  
<asp:Content ID="indexContent" ContentPlaceHolderID="MainContent" runat="server">  
    <p>  
        To learn more about this website, click the following link:   
        <%= Html.ActionLink("About this Website", "About" ) %>  
    </p>  
</asp:Content>  
"Aboud this Website”显示的内容,"About” Action的名字
生成的Html如下:
<a href="/Home/About">About this Website</a>
ActionLink可以添加接受很多参数

· linkText – 链接上的文字

· actionName – 链接目标的action名字

· routeValues – 通向action的route值

· controllerName – controller名字

· htmlAttributes – 链接的html属性

· protocol – 链接协议 (比如:https)

· hostname – 链接的Host名字 (比如:www.MyWebsite.com)

· fragment – 这个还没弄的太明白╮(╯▽╰)╭

如果想添加个图片链接,使用Url.Action:

<a href="<%= Url.Action("Delete") %>"><img src="http://www.cnblogs.com/Content/Delete.png" alt="Delete" style="border:0px" /></a>
Html Helper还可以生成很多Html控件:

· BeginForm()

· CheckBox()

· DropDownList()

· EndForm()

· Hidden()

· ListBox()

· Password()

· RadioButton()

· TextArea()

· TextBox()

基本上看名字就知道了,看例子:

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<MvcApplication1.Models.Customer>" %>  
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">  
  
    <%= Html.ValidationSummary("Create was unsuccessful. Please correct the errors and try again.") %>  
  
    <% using (Html.BeginForm()) {%>  
  
        <fieldset>  
            <legend>Register</legend>  
            <p>  
                <label for="FirstName">First Name:</label>  
                <%= Html.TextBox("FirstName") %>  
                <%= Html.ValidationMessage("FirstName", "*") %>  
            </p>  
            <p>  
                <label for="LastName">Last Name:</label>  
                <%= Html.TextBox("LastName") %>  
                <%= Html.ValidationMessage("LastName", "*") %>  
            </p>  
            <p>  
                <label for="Password">Password:</label>  
                <%= Html.Password("Password") %>  
                <%= Html.ValidationMessage("Password", "*") %>  
            </p>  
            <p>  
                <label for="Password">Confirm Password:</label>  
                <%= Html.Password("ConfirmPassword") %>  
                <%= Html.ValidationMessage("ConfirmPassword", "*") %>  
            </p>  
            <p>  
                <label for="Profile">Profile:</label>  
                <%= Html.TextArea("Profile", new {cols=60, rows=10})%>  
            </p>  
            <p>  
                <%= Html.CheckBox("ReceiveNewsletter") %>  
                <label for="ReceiveNewsletter" style="display:inline">Receive Newsletter?</label>  
            </p>  
            <p>  
                <input type="submit" value="Register" />  
            </p>  
        </fieldset>  
  
    <% } %>  
  
</asp:Content>  

其中Html.BeginForm()和EndForm()要单独说一下:默认情况下,它会指向和自己相同的action,但也会接受不同参数改变指向的action:

· routeValues -- 如上

· actionName – 如上

· controllerName – 如上

· method – 只能使用POST和GET,必须使用javascript

· htmlAttributes – 如上

.Encode(),这个就是替换<为&lt; >为&gt;等等

.AntiForgeryToken 这个是为了抵御跨域攻击的。

<%= Html.AntiForgeryToken() %>

会生成一个隐藏域,value是每次都不同的随机字符串,如:

<input name="__RequestVerificationToken"  type="hidden"value="6tbg3PWU9oAD3bhw6jZwxrYRyWPhKede87K/PFgaw
     6MI3huvHgpjlCcPzDzrTkn8" />
helper会创建一个cookie和这个隐藏域的值相比较
在Controller中如下写代码就可以了:
using System.Web.Mvc;  
  
namespace MvcApplication1.Controllers  
{  
    public class BankController : Controller  
    {  
        //  
        // GET: /Bank/Withdraw  
  
        public ActionResult Withdraw()  
        {  
            return View();  
        }  
  
        //  
        // POST: /Bank/Withdraw  
        [AcceptVerbs(HttpVerbs.Post)]  
        [ValidateAntiForgeryToken]  
        public ActionResult Withdraw(decimal amount)  
        {  
            // Perform withdrawal  
            return View();  
        }  
  
    }  
} 

创建自己的HTML Helpers

using System;  
using System.Web.Mvc;  
  
namespace Helpers  
{  
    public static class SubmitButtonHelper  
    {  
        /// <summary>  
        /// Renders an HTML form submit button  
        /// </summary>  
        public static string SubmitButton(this HtmlHelper helper, string buttonText)  
        {  
            return String.Format("<input type=\"submit\" value=\"{0}\" />", buttonText);  
        }  
  
    }  
}  

这样就名了吧,创建一个submit.(*^__^*)

这样可以创建很复杂的Html格式的。发挥想象

ps:代码还是别人的