asp.net mvc htmlHelper
ASP.NET MVC 3.0 HTML辅助方法
HTML辅助方法(html helper)是用来帮助生成HTML的方法。
1、HTML辅助方法应用实例
◊ 生成form元素
@using (Html.BeginForm("About", "Home")) {
@Html.TextBox("ProductName")
}
生成的html代码如下:
<form method="post" action="/Home/About">
<input id="ProductName" type="text" value="" name="ProductName">
</form>
◊ 生成TextBox元素
@Html.TextBox("ProductName", "产品名称", new { id = "txtProductName", @class = "txt" })
public static MvcHtmlString TextBox(this HtmlHelper htmlHelper, string name, object value, object htmlAttributes);
生成html代码如下:
<input id="txtProductName" class="txt" type="text" value="产品名称" name="ProductName">
@Html.TextBox("ProductName", "产品名称", ViewBag.Attributes as IDictionary<string, object>)
public static MvcHtmlString TextBox(this HtmlHelper htmlHelper, string name, object value, IDictionary<string, object> htmlAttributes);

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc; using System.Collections; namespace MvcTest.Controllers
{
public class HomeController : Controller
{
//
// GET: /Home/ public ActionResult Index()
{
IDictionary<string, object> attr = new Dictionary<string, object>();
attr.Add("id", "txtProductName");
attr.Add("style", "border:1px solid #666666;");
attr.Add("class", "txt");
ViewBag.Attributes = attr; return View();
} }
}

生成的html代码:
<input id="txtProductName" class="txt" type="text" value="产品名称" style="border:1px solid #666666;" name="ProductName">
◊ 生成DropDownList

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc; using System.Collections; namespace MvcTest.Controllers
{
public class HomeController : Controller
{
//
// GET: /Home/ public ActionResult Index()
{
List<SelectListItem> lst = new List<SelectListItem>();
lst.Add(new SelectListItem { Text = "数码电子", Value = "1" });
lst.Add(new SelectListItem { Text = "服装服饰", Value = "2" });
lst.Add(new SelectListItem { Text = "珠宝首饰", Value = "3" });
ViewBag.Category = lst;
ViewBag.Category = new SelectList(lst, "Value", "Text", "2"); return View();
} }
}

@Html.DropDownList("Category", "请选择")
生成的HTML代码:

<select id="Category" name="Category">
<option value="">请选择</option>
<option value="1">数码电子</option>
<option selected="selected" value="2">服装服饰</option>
<option value="3">珠宝首饰</option>
</select>


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc; using System.Collections; namespace MvcTest.Controllers
{
public class HomeController : Controller
{
//
// GET: /Home/ public ActionResult Index()
{
List<Models.Province> lst = new List<Models.Province>();
lst.Add(new Models.Province { ProvinceID = 1, ProvinceNo = "100000", ProvinceName = "北京" });
lst.Add(new Models.Province { ProvinceID = 2, ProvinceNo = "110000", ProvinceName = "上海" });
lst.Add(new Models.Province { ProvinceID = 3, ProvinceNo = "120000", ProvinceName = "深圳" });
ViewBag.Category = new SelectList(lst, "ProvinceNo", "ProvinceName", "110000");
return View();
} }
}

2、自定义HTML辅助方法

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web; using System.Web.Routing;
using System.Web.Mvc; namespace MvcTest.html
{
public static class HtmlExtensions
{
public static MvcHtmlString Img(this HtmlHelper htmlHelper, string src)
{
return Img(htmlHelper, String.Empty, src, String.Empty, null);
}
public static MvcHtmlString Img(this HtmlHelper htmlHelper, string id, string src)
{
return Img(htmlHelper, id, src, String.Empty, null);
}
public static MvcHtmlString Img(this HtmlHelper htmlHelper, string id, string src, string alt, object htmlAttributes)
{
TagBuilder builder = new TagBuilder("img");
builder.GenerateId(id);
builder.MergeAttribute("src", src);
builder.MergeAttribute("alt", alt);
builder.MergeAttributes(new RouteValueDictionary(htmlAttributes)); return MvcHtmlString.Create(builder.ToString(TagRenderMode.SelfClosing));
}
}
}


@{
ViewBag.Title = "Index";
Layout = "~/Views/Shared/_Layout.cshtml";
}
@using MvcTest.html
@Html.Img("", Url.Content("~/Content/logo.png"))
@Html.Img("imgLogo", Url.Content("~/Content/logo.png"), "Logo", new { border = "1px solid #666666", @class = "c01" })

生成的HTML代码:
<img alt="" src="/Content/logo.png" />
<img alt="Logo" border="1px solid #666666" class="c01" id="imgLogo" src="/Content/logo.png" />
asp.net mvc htmlHelper的更多相关文章
- ASP.NET MVC HtmlHelper用法集锦
ASP.NET MVC HtmlHelper用法集锦 在写一个编辑数据的页面时,我们通常会写如下代码 1:<inputtype="text"value='<%=View ...
- 扩展ASP.NET MVC HtmlHelper类
在这篇帖子中我会使用一个示例演示扩展ASP.NET MVC HtmlHelper类,让它们可以在你的MVC视图中工作.这个示例中我会提供一个简单的方案生成Html表格. HtmlHelper类 Htm ...
- ASP .NET MVC HtmlHelper扩展——简化“列表控件”的绑定
在众多表单元素中,有一类<select>元素用于绑定一组预定义列表.传统的ASP.NET Web Form中,它对应着一组重要的控件类型,即ListControl,我们经常用到DropDo ...
- ASP.NET MVC HtmlHelper 类的扩展方法
再ASP.NET MVC编程中用到了R语法,在View页面编辑HTML标签的时候,ASP.NET MVC 为我们准备好了可以辅助我们写这些标签的办法,它们就是HtmlHelper.微软官方地址是:ht ...
- [转]ASP.NET MVC HtmlHelper扩展之Calendar日期时间选择
本文转自:http://blog.bossma.cn/asp_net_mvc/asp-net-mvc-htmlhelper-calendar-datetime-select/ 这里我们扩展HtmlHe ...
- C# ASP.NET MVC HtmlHelper用法大全
UrlHrlper 下面的两个地址一样的功能 下边这个防止路由规则改变 比如UserInfo/Index改为UserInfo-Index,使用下面的不受影响 另一种形式的超链接: <%: Htm ...
- asp.net mvc Htmlhelper简单扩展
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.We ...
- ASP.NET MVC HtmlHelper之Html.ActionLink
前言 ActionLink用于生成超链接,方法用于指向Controller的Action. 扩展方法与参数说明 ActionLink扩展方法如下: public static MvcHtmlStrin ...
- ASP.NET MVC HtmlHelper用法大全
HTML扩展类的所有方法都有2个参数: 以textbox为例子public static string TextBox( this HtmlHelper htmlHelper, string name ...
随机推荐
- Backbone☞View中的events...click事件失效
<div id="container"> <input type="button" id="test_click" val ...
- CentOS编译安装Apache 2.4.x时报错:configure: error: Bundled APR requested but not found at ./srclib/. Download and unpack the corresponding apr and apr-util packages to ./srclib/.
先前按照这篇文章“CentOS6.x编译安装LAMP(2):编译安装 Apache2.2.22”去编译安装Apache2.2.x版本时,安装得挺顺利,今天换成Apache2.4.x版本,安装方法一样, ...
- 如何查看 Linux是32位还是64位?
方法一:执行命令 file /sbin/init [root@localhost jianbao]# file /sbin/init /sbin/init: ELF 32-bit LSB shared ...
- redhat 中安装rpm包时遇到异常 “error: Failed dependencies:xinetd is needed by .”
redhat 中安装rpm包时遇到错误 “error: Failed dependencies:xinetd is needed by ....” redhat中安装rpm包时遇到“error: Fa ...
- PHPCMS系统使用的弹出窗口插件artDialog
来源: http://aui.github.io/artDialog/doc/index.html (官方) http://lab.seaning.com/ http://www.mb5u.com/ ...
- 联不上网 Unable to initialize Windows Sockets interface. General failure.
电脑莫名联不上网 Unable to initialize Windows Sockets interface. General failure. Unable to initialize the W ...
- Java 的printf(转)
出处:http://blog.csdn.net/swandragon/article/details/4653600 public class TestPrintf{public static voi ...
- C语言文件操作
C语言文件操作,以下以基本的例子和说明来展开怎么通过C语言来进行文件操作. 操作文件,我们得需要知道什么?当然是路径和文件名. 首先我需要知道我操作的文件在哪里,叫什么名字.在C语言中还存在一个打开方 ...
- Hello 2016
Hello 2016 I am really happy to work and study here. Nothing is better than be oneself ! It's import ...
- No enum constant org.apache.ibatis.type.JdbcType.xxx 错误
配置文件Mapper中JdbcType错误,有可能是大小写,有可能是拼写错误