Asp.Net MVC 学习心得 之 Html Helper
来源:互联网 发布:google keep windows 编辑:程序博客网 时间:2024/04/29 17:46
首先使用Asp.NetMVC可以不使用HTML Helper,不过使用了Html Helper可以节约很多时间的O(∩_∩)O~
一、标准Html Helper
.ActionLink
创建一个链接,但现在还不能创建一个带图片的链接
<%@ Page Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage" %><asp:ContentID="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如下:
<ahref="/Home/About">About this Website</a>
ActionLink可以添加接受很多参数
· linkText – 链接上的文字
· actionName – 链接目标的action名字
· routeValues – 通向action的route值
· controllerName – controller名字
· htmlAttributes – 链接的html属性
· protocol – 链接协议 (比如:https)
· hostname – 链接的Host名字 (比如:http://www.52mvc.com)
· fragment – 这个还没弄的太明白╮(╯▽╰)╭
如果想添加个图片链接,使用Url.Action:
<ahref="<%= Url.Action("Delete") %>"><imgsrc="http://blog.csdn.net/jhl52771/article/details/8495991"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:ContentID="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><labelfor="FirstName">First Name:</label><%= Html.TextBox("FirstName")%><%= Html.ValidationMessage("FirstName","*")%></p><p><labelfor="LastName">Last Name:</label><%= Html.TextBox("LastName")%><%= Html.ValidationMessage("LastName","*")%></p><p><labelfor="Password">Password:</label><%= Html.Password("Password")%><%= Html.ValidationMessage("Password","*")%></p><p><labelfor="Password">Confirm Password:</label><%= Html.Password("ConfirmPassword")%><%= Html.ValidationMessage("ConfirmPassword","*")%></p><p><labelfor="Profile">Profile:</label><%= Html.TextArea("Profile",new{cols=60, rows=10})%></p><p><%= Html.CheckBox("ReceiveNewsletter")%><labelfor="ReceiveNewsletter"style="display:inline">Receive Newsletter?</label></p><p><inputtype="submit"value="Register"/></p></fieldset><%}%></asp:Content>
其中Html.BeginForm()和EndForm()要单独说一下:默认情况下,它会指向和自己相同的action,但也会接受不同参数改变指向的action:
· routeValues -- 如上
· actionName – 如上
· controllerName – 如上
· method – 只能使用POST和GET,必须使用javascript
· htmlAttributes – 如上
.Encode(),这个就是替换<为< >为>等等
.AntiForgeryToken这个是为了抵御跨域攻击的。
<%= Html.AntiForgeryToken()%>
<inputname="__RequestVerificationToken"type="hidden"value="6tbg3PWU9oAD3bhw6jZwxrYRyWPhKede87K/PFgaw
6MI3huvHgpjlCcPzDzrTkn8"/>
helper会创建一个cookie和这个隐藏域的值相比较
在Controller中如下写代码就可以了:
usingSystem.Web.Mvc;namespaceMvcApplication1.Controllers
{publicclassBankController : Controller
{//// GET: /Bank/WithdrawpublicActionResult Withdraw()
{returnView();
}//// POST: /Bank/Withdraw[AcceptVerbs(HttpVerbs.Post)]
[ValidateAntiForgeryToken]publicActionResult Withdraw(decimalamount)
{// Perform. withdrawalreturnView();
}
}
}
创建自己的HTML Helpers
usingSystem;usingSystem.Web.Mvc;namespaceHelpers
{publicstaticclassSubmitButtonHelper
{/// <summary>/// Renders an HTML form. submit button/// </summary>publicstaticstringSubmitButton(thisHtmlHelper helper,stringbuttonText)
{returnString.Format("<input type=\"submit\" value=\"{0}\" />", buttonText);
}
}
}
这样就名了吧,创建一个submit.(*^__^*)
这样可以创建很复杂的Html格式的。发挥想象
· method – 只能使用POST和GET,必须使用javascript· htmlAttributes – 如上 .Encode(),这个就是替换<为< >为>等等.AntiForgeryToken这个是为了抵御跨域攻击的。<%= Html.AntiForgeryToken()%><inputname="__RequestVerificationToken"type="hidden"value="6tbg3PWU9oAD3bhw6jZwxrYRyWPhKede87K/PFgaw 6MI3huvHgpjlCcPzDzrTkn8"/>helper会创建一个cookie和这个隐藏域的值相比较在Controller中如下写代码就可以了:usingSystem.Web.Mvc;namespaceMvcApplication1.Controllers {publicclassBankController : Controller {//// GET: /Bank/WithdrawpublicActionResult Withdraw() {returnView(); }//// POST: /Bank/Withdraw[AcceptVerbs(HttpVerbs.Post)] [ValidateAntiForgeryToken]publicActionResult Withdraw(decimalamount) {// Perform. withdrawalreturnView(); } } }创建自己的HTML HelpersusingSystem;usingSystem.Web.Mvc;namespaceHelpers {publicstaticclassSubmitButtonHelper {/// <summary>/// Renders an HTML form. submit button/// </summary>publicstaticstringSubmitButton(thisHtmlHelper helper,stringbuttonText) {returnString.Format("<input type=\"submit\" value=\"{0}\" />", buttonText); } } }这样就名了吧,创建一个submit.(*^__^*)这样可以创建很复杂的Html格式的。发挥想象
- Asp.Net MVC 学习心得 之 Html Helper
- Asp.net mvc 创建自定义HTML Helper
- ASP.NET MVC 了解 HTML Helper
- MVC 之 Html Helper
- ASP.NET MVC Helper Method
- ASP.NET MVC @helper使用说明
- [ASP.NET MVC 小牛之路]13 - Helper Method
- [ASP.NET MVC 小牛之路]13 - Helper Method
- [ASP.NET MVC 小牛之路]13 - Helper Method
- Asp.net MVC 学习心得
- .net Mvc HTML Helper控件中的htmlAttributes
- Asp.net Mvc Framework 八 (Helper)
- Asp.net Mvc Framework 八 (Helper)
- ASP.NET MVC控件辅助工具(Helper)
- ASP.NET MVC Toolkit - how to use the Html Helper class
- Asp.Net MVC HTML Helper 中 LabelFor TextBoxFor的一些理解
- ASP.NET MVC学习之 Html.ActionLink
- ASP.NET MVC学习之 Html.ActionLink
- 移植oracle数据库表结构到mysql
- 纯技术分析阿里云OS和Android之间的关系(转)
- 新浪微博首页的头部标题
- (强水过) Eqs (P1840)
- C嵌套python脚本调用心得
- Asp.Net MVC 学习心得 之 Html Helper
- 5G WiFi时代,802.11ac设备面临的大批量测试挑战(转)
- ios中NSUserDefaults的使用
- 【iOS开发必收藏】详解iOS应用程序内使用IAP/StoreKit付费、沙盒(SandBox)测试、创建测试账号流程!【2012-12-11日更新获取"产品付费数量等于0的问题"】
- object-c开发中混合使用或不使用ARC
- 每个程序员都应该知道的福利
- 《离散数学及其应用》学习(上)
- C++Primer再读读书笔记:第三章
- scsi驱动模型