asp.net html helper,Asp.Net MVC 学习心得 之 Html Helper

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

一、标准Html Helper

.ActionLink

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

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如下:About this Websitea>ActionLink可以添加接受很多参数

· linkText – 链接上的文字

· actionName – 链接目标的action名字

· routeValues – 通向action的route值

· controllerName – controller名字

· htmlAttributes – 链接的html属性

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

· hostname – 链接的Host名字 ()

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

如果想添加个图片链接,使用Url.Action:">Deletea>Html Helper还可以生成很多Html控件:

· BeginForm()

· CheckBox()

· DropDownList()

· EndForm()

· Hidden()

· ListBox()

· Password()

· RadioButton()

· TextArea()

· TextBox()

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

= Html.ValidationSummary("Create was unsuccessful. Please correct the errors and try again.") %>

using (Html.BeginForm()) {%>

Registerlegend>

First Name:label>

= Html.TextBox("FirstName") %>

= Html.ValidationMessage("FirstName", "*") %>

p>

Last Name:label>

= Html.TextBox("LastName") %>

= Html.ValidationMessage("LastName", "*") %>

p>

Password:label>

= Html.Password("Password") %>

= Html.ValidationMessage("Password", "*") %>

p>

Confirm Password:label>

= Html.Password("ConfirmPassword") %>

= Html.ValidationMessage("ConfirmPassword", "*") %>

p>

Profile:label>

= Html.TextArea("Profile", new {cols=60, rows=10})%>

p>

= Html.CheckBox("ReceiveNewsletter") %>

Receive Newsletter?label>

p>

p>

fieldset>

} %>

asp:Content>

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

· routeValues -- 如上

· actionName – 如上

· controllerName – 如上

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

· htmlAttributes – 如上

.Encode(),这个就是替换为>等等

.AntiForgeryToken这个是为了抵御的。= Html.AntiForgeryToken() %>

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 Helpersusing System;

using System.Web.Mvc;

namespace Helpers

{

public static class SubmitButtonHelper

{

///

/// Renders an HTML form. submit button

///

public static string SubmitButton(this HtmlHelper helper, string buttonText)

{

return String.Format("", buttonText);

}

}

}

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

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值