MVC4 擴充方法

來源:互聯網
上載者:User

標籤:style   blog   color   os   io   art   for   re   

1. 擴充代碼機構

namespace System.Web.Mvc.Html{    public static class FormExtensions//表單相關擴充方法,例如建立表單標籤等。    public static class InputExtensions//這裡包含了所有input,例如:text,button,readiobutton等等。    public static class LinkExtensions//連結相關方法    public class MvcForm : IDisposable//與用戶端控制項無關    public static class RenderPartialExtensions//這是輸出PartialView    public static class SelectExtensions//輸出下拉框    public static class TextAreaExtensions//輸出多行文字框    public static class ValidationExtensions//輸出相關表單元素驗證。}

 

2. TagBuilder 結構

    // Methods    public TagBuilder(string tagName);    public void AddCssClass(string value);//增加樣式    public void GenerateId(string name);//設定控制項ID    private string GetAttributesString();    public void MergeAttribute(string key, string value);//設定屬性值    public void MergeAttribute(string key, string value, bool replaceExisting);    public void MergeAttributes<TKey, TValue>(IDictionary<TKey, TValue> attributes);    public void MergeAttributes<TKey, TValue>(IDictionary<TKey, TValue> attributes, bool replaceExisting);    public void SetInnerText(string innerText);//設定顯示文本    public override string ToString();    public string ToString(TagRenderMode renderMode);//輸出控制項html

 

3. 樣本: Html.Image(...)

public static class ImageHelper{        public static string Image(this HtmlHelper helper, string id, string url, string alternateText)        {            return Image(helper, id, url, alternateText, null);        }        public static string Image(this HtmlHelper helper, string id, string url, string alternateText, object htmlAttributes)        {            // 建立IMG標籤            var builder = new TagBuilder("img");            // 增加ID屬性            builder.GenerateId(id);            // 增加屬性            builder.MergeAttribute("src", url);            builder.MergeAttribute("alt", alternateText);            builder.MergeAttributes(new RouteValueDictionary(htmlAttributes));            // 輸出完整的img標籤            return builder.ToString(TagRenderMode.SelfClosing);        }}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.