3.案例三,ajax页面传递值可以使用get方式,或者post方式的传递JSON格式数据转换
简单的转换一个get传递的字符串Name=xx&Age=xx,后台直接用一个字典去模拟了:
Dictionary<string, object> dic = new Dictionary<string, object>();
dic.Add("Id",100);
dic.Add("Name", "keso");
dic.Add("Group", "程序员");
转换字典方法:
public static T ConvertDic<T>(Dictionary<string, object> dic) { T model = Activator.CreateInstance<T>(); PropertyInfo[] modelPro = model.GetType().GetProperties(); if (modelPro.Length > 0 && dic.Count() > 0) { for (int i = 0; i < modelPro.Length; i++) { if (dic.ContainsKey(modelPro[i].Name)) { modelPro[i].SetValue(model, dic[modelPro[i].Name], null); } } } return model; } |
最后的调用:
User user = ConvertDic<User>(dic);