zoukankan      html  css  js  c++  java
  • ASP.NET WebForm Form表单如何实现MVC那种“自动装配”效果呢?

    我们知道ASP.NET MVC有个强大的地方就是Form表单提交到action的时候,可以直接将Form的参数直接装配到action的参数实体对象中

    比如

    action方法 Register(UserModel userModel)

    {

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

    }

    在提交表单的时候,会自动讲表单里面的字段封装到对应的UserModel字段里面

    那么 WebForm里面可不可以也紫将呢?

    因为每次都要去获得数据,优秀的程序员应该要学会代码封装,代码复用,重复的工作不要做

    我们其实可以利用反射来实例化对象的(自动装配)北盟网www.bamn.cn

    好了废话不多....

    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);


    好了,感谢你的阅读,希望对你有帮助了,
    另外 本博客将会有一些原创的视频教程分享给大家,感谢大家的支持


  • 相关阅读:
    项目常见异常
    mysql 存储过程中使用事物+事件定时执行存储过程
    Spring Mvc 配置 之 ContextLoaderListener
    Spring Boot 之 https
    Spring Boot 之 annotation注解
    用python打印99乘法口诀表
    gerrit代码审核工具之“error unpack failed error Missing unknown”错误解决思路
    在eclipse搭建python开发环境
    python2与python3语法区别之_重定向
    2_jenkins_git创建创建及项目构建
  • 原文地址:https://www.cnblogs.com/maijin/p/4559111.html
Copyright © 2011-2022 走看看