ASP.NET中實現Form表單欄位值自動填滿到操作模型中_實用技巧

來源:互聯網
上載者:User

我們知道ASP.NET MVC有個強大的地方就是Form表單提交到action的時候,可以直接將Form的參數直接裝配到action的參數實體對象中

比如

複製代碼 代碼如下:

action方法 Register(UserModel userModel)

{

   ............................. 

}


在提交表單的時候,會自動講表單裡面的欄位封裝到對應的UserModel欄位裡面

那麼 WebForm裡面可不可以也紫將呢?

因為每次都要去獲得資料,優秀的程式員應該要學會代碼封裝,代碼複用,重複的工作不要做

我們其實可以利用反射來執行個體化對象的(自動裝配)

好了廢話不多....

pageload裡面很簡單了

複製代碼 代碼如下:

protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPost())
            {
                InitPage();//第一次訪問呈現頁面
            }
            else
            {
                UserModel userModel = AssembleModel<UserModel>(base.valueCollection);
            }
        }

關鍵就是基類裡面的AssembleModel 方法了

基類裡面

我們首先擷取到內容相關的參數 IT404

複製代碼 代碼如下:

protected NameValueCollection valueCollection = HttpContext.Current.Request.Params;

基類很簡單,就是將內容相關的提交的參數存放到valueCollection

然後再看AssembleModel方法了,這是一個泛型方法

複製代碼 代碼如下:

/// <summary>
        /// 反射擷取類的屬性
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        protected PropertyInfo[] GetPropertyInfoArray(Type type)
        {
            PropertyInfo[] props = null;
            try
            {
                object obj = Activator.CreateInstance(type);
                props = type.GetProperties(BindingFlags.Public | BindingFlags.Instance);
            }
            catch (Exception ex)
            {

            }
            return props;
        }

        /// <summary>
        /// 根據NameValueCollection 自動裝配
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="valueCollection"></param>
        /// <returns></returns>
        protected T AssembleModel<T>(NameValueCollection valueCollection)
        {
            PropertyInfo[] propertyInfoList = GetPropertyInfoArray(typeof(T));
            object obj = Activator.CreateInstance(typeof(T), null);//建立指定類型執行個體
            foreach (string key in valueCollection.Keys)//所有內容相關的值
            {
                foreach (var PropertyInfo in propertyInfoList)//所有實體屬性
                {
                    if (key.ToLower() == PropertyInfo.Name.ToLower())
                    {
                        PropertyInfo.SetValue(obj, valueCollection[key], null);//給對象賦值
                    }
                }
            }
            return (T)obj;
        }

很簡單,就是遍曆參數,然後用反射遍曆出實體類的共有屬性,然後根據名字name來匹配和賦值

所以以後我們只需要一句代碼 就能自動裝配上從用戶端存過來的值了

複製代碼 代碼如下:

UserModel userModel = AssembleModel<UserModel>(base.valueCollection);

聯繫我們

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