目前分類:ASP.NET MVC (5)

瀏覽方式: 標題列表 簡短摘要

讓enum變成view來使用好像滿頻繁的,記錄一下擴充dropdownlist

public static MvcHtmlString DropDownEnumList<T>(this HtmlHelper htmlHelper, string name, string selectValue)
            where T : struct, IConvertible
        {
            return htmlHelper.DropDownList(name, GetSelectEnumListItems<T>(selectValue));
        }

        public static MvcHtmlString DropDownEnumList<T>(this HtmlHelper htmlHelper, string name,string selectValue, object htmlAttributes)
            where T : struct, IConvertible 
        {
            return htmlHelper.DropDownList(name, GetSelectEnumListItems<T>(selectValue), htmlAttributes);
        }

        public static MvcHtmlString DropDownEnumList<T>(this HtmlHelper htmlHelper, string name,string selectValue, IDictionary<string, object> htmlAttributes)
            where T : struct, IConvertible 
        {
            return htmlHelper.DropDownList(name, GetSelectEnumListItems<T>(selectValue), htmlAttributes);
        }

        public static MvcHtmlString DropDownEnumList<T>(this HtmlHelper htmlHelper, string name,string selectValue, string optionLabel)
            where T : struct, IConvertible 
        {
            return htmlHelper.DropDownList(name, GetSelectEnumListItems<T>(selectValue), optionLabel);
        }

        public static MvcHtmlString DropDownEnumList<T>(this HtmlHelper htmlHelper, string name, string selectValue, string optionLabel, object htmlAttributes)
            where T : struct, IConvertible 
        {
            return htmlHelper.DropDownList(name, GetSelectEnumListItems<T>(selectValue), optionLabel, htmlAttributes);
        }

        public static MvcHtmlString DropDownEnumList<T>(this HtmlHelper htmlHelper, string name,string selectValue, string optionLabel, IDictionary<string, object> htmlAttributes)
            where T : struct, IConvertible 
        {
            return htmlHelper.DropDownList(name, GetSelectEnumListItems<T>(selectValue), optionLabel, htmlAttributes);
        }

        public static IEnumerable<SelectListItem> GetSelectEnumListItems<T>(string selectValue)
            where T : struct, IConvertible
        {
            List<SelectListItem> items = new List<SelectListItem>();
            foreach (var e in Enum.GetNames(typeof(T)))
            {
                string val = ((int)Enum.Parse(typeof(T), e)).ToString();
                var selectlistItem = new SelectListItem() { Text = e, Value = val };
                if (!string.IsNullOrWhiteSpace(selectValue) && (val == selectValue))
                {
                    selectlistItem.Selected = true;
                }
                items.Add(selectlistItem);
            }
            return items;
        }

Jimmy 發表在 痞客邦 留言(0) 人氣()

搞了老半天,為了接JavaScript的Json格式,轉成Server端(C#)的Dictionary集合會有問題, List集合好像沒問題,先用這個方式解決,此範例模疑一個Model裡有這2種集合傳遞。

JQuery:

文章標籤

Jimmy 發表在 痞客邦 留言(0) 人氣()

久違ASP.NET MVC,記錄一下For MVC的驗証碼產生器。

Model

文章標籤

Jimmy 發表在 痞客邦 留言(0) 人氣()

使用ASP.NET MVC時,像比較複雜的UI設計,可以考慮一下這個Tools喔,把時間花在系統隱定上囉~~哈哈!!!

  1. Calendar : 月曆選譯器

文章標籤

Jimmy 發表在 痞客邦 留言(0) 人氣()

ASP.NET MVC 中有一些常用的視圖引擎,這裡做個整理列表。

以下命名空間 System.Web.Mvc.Html 中:

文章標籤

Jimmy 發表在 痞客邦 留言(0) 人氣()