asp.net mvc htmlHelper

來源:互聯網
上載者:User

標籤:

  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  src="/Content/logo.png" /><img alt="Logo" border="1px solid #666666" class="c01" id="imgLogo" src="/Content/logo.png" />

 

asp.net mvc htmlHelper

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.