FormToModel 转换错误
小大飞 2013-12-20 02:35:16 <input type="text" name="txtProName" size="30" value="<%=model.ProName %>" class="validate[required]" />
--------------------------------------------------类begin
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using System.Web.UI.HtmlControls;
using System.Web.UI;
using System.Collections.Specialized;
namespace LSSystem.Tools
{
/// <summary>
/// 对象赋值与表单绑值
/// </summary>
/// <typeparam name="T"></typeparam>
public static class FormToModel<T>
{
#region 将表单集合赋给实体对象
/// <summary>
/// 将表单集合赋给实体对象
/// </summary>
/// <param name="t">实体对象</param>
/// <param name="form">表单集合</param>
public static void SetModel(T t, NameValueCollection form)
{
Type type = t.GetType();
PropertyInfo[] pi = type.GetProperties();
foreach (PropertyInfo p in pi)
{
if (form["txt" + p.Name] != null)
{
p.SetValue(t, Convert.ChangeType(form["txt" + p.Name], p.PropertyType), null);
}
}
}
#endregion
#region 将对象赋予绑定到表单
/// <summary>
/// 将对象赋予绑定到表单
/// </summary>
/// <param name="t">实体对象</param>
/// <param name="c">页面对象</param>
public static void BindFormValue(T t, Page page)
{
Type type = t.GetType();
PropertyInfo[] pi = type.GetProperties();
foreach (PropertyInfo p in pi)
{
HtmlInputText input = page.FindControl("txt" + p.Name) as HtmlInputText;
HtmlTextArea text = page.FindControl("txt" + p.Name) as HtmlTextArea;
if (input != null)
{
input.Value = p.GetValue(t, null).ToString();
}
else if (text != null)
{
text.InnerText = p.GetValue(t, null).ToString();
}
}
}
#endregion
}
}
------------------------------------------------------类end
后台用这个FormToModel<Model.ProductManage>.SetModel(model, Request.Form);
接收用户的输入,直接转成实体 。
然后出现了错误,怎么办?