效果图:
视图代码:
<form action="form.aspx" method="post"> <input type="text" name="card" /><p /> <input type="text" name="pwd" /><p /> <input type="text" name="status" /><p /> <input type="submit" value="input" /> </form>
功能代码:
protected void Page_Load(object sender, EventArgs e) { admin a = new admin(); GetFormToM<admin>(ref a, Request.Form); //获取表单的值 OutT<admin>(a); //输出表单的值 } private class admin { public string card { set; get; } public string pwd { set; get; } public int status { set; get; } } //模型 static void GetFormToM<T>(ref T m,NameValueCollection form) { Type t = m.GetType(); PropertyInfo[] pi = t.GetProperties(); foreach(PropertyInfo p in pi) { if(form[p.Name] != null ) { p.SetValue(m, Convert.ChangeType(form[p.Name], p.PropertyType), null); } } } //获得表单 static void OutT<T>(T m) { Type t = m.GetType(); PropertyInfo[] pi = t.GetProperties(); foreach(var p in pi) { HttpContext.Current.Response.Write(p.Name + " = " + p.GetValue(m)+"<p/>"); } } //输出模型
原文:http://www.cnblogs.com/0to9/p/5107915.html