帮我优化2个方法

todayclock 2011-09-07 07:13:16
通过反射获取表单提交的值。
前提是:form表单元素的值和属性名称是相同的。

//由form获取实体
public static T GetFormValue<T>(HttpRequest reqeust)
{
Type modelType = typeof(T);
T model = Activator.CreateInstance<T>();
foreach (string key in reqeust.Form.AllKeys)
{
foreach (PropertyInfo pi in modelType.GetProperties())
{
if (pi.Name.Equals(key, StringComparison.InvariantCultureIgnoreCase))
{
string value = reqeust.Form[key];
pi.SetValue(model, HackType(value, pi.PropertyType), null);
}
}
}
return model;
}
//类型转换(网上找的。)
private static object HackType(object value, Type conversionType)
{
if (conversionType.IsGenericType && conversionType.GetGenericTypeDefinition().Equals(typeof(Nullable<>)))
{
if (value == null)
return null;

System.ComponentModel.NullableConverter nullableConverter = new System.ComponentModel.NullableConverter(conversionType);
conversionType = nullableConverter.UnderlyingType;
}
return Convert.ChangeType(value, conversionType);
}
...全文
136 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
HDZC 2011-09-08
  • 打赏
  • 举报
回复
private static T HackType<T, TK>(TK value)
{
try
{
return (T)Convert.ChangeType(value, typeof(T), CultureInfo.InvariantCulture);
}
catch
{
return default(T);
}
}
todayclock 2011-09-08
  • 打赏
  • 举报
回复
对于MVC那种强类型视图里。用的MVC控件,能自动赋值,
他们源码中是根据传值显示出来的。比如:

case InputType.Password:
if (value != null) {
tagBuilder.MergeAttribute("value", valueParameter, isExplicitValue);
}
break;

但在2.0中我找啥赋值呢!
<input type="text" name="Title" value="<%=article.Title %>"> ???
CalvinR 2011-09-08
  • 打赏
  • 举报
回复
八楼主的方法很好
todayclock 2011-09-08
  • 打赏
  • 举报
回复
Page.FindControl只能找runat="server"的控件
找下面的那些控件,是没希望。
[Quote=引用 1 楼 todayclock 的回复:]
<form id="form1" action="TestSubmitForm.aspx" method="post">
<input name="subFlag" name="subFlag" type="hidden" value="1" />
<input name="Title" type="text" /><br />
<input name="Content" type="text" /><br />
<input name="Author" type="text" /><br />
<input name="Button1" type="submit" value="button" />
</form>
[/Quote]
todayclock 2011-09-08
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 liuchaolin 的回复:]
……
[/Quote]
谢了。
天府荣城 2011-09-08
  • 打赏
  • 举报
回复
八楼不错,帮顶了。
沈勇 2011-09-08
  • 打赏
  • 举报
回复
纯粹帮顶了,太晚了,我不是夜猫
md5e 2011-09-08
  • 打赏
  • 举报
回复
第一个方法没有必要写两个循环


public static T GetFormValue<T>(HttpRequest reqeust)
{
Type modelType = typeof(T);
T model = Activator.CreateInstance<T>();
foreach (PropertyInfo pi in modelType.GetProperties())
{
if(reqeust.Form[pi.Name]!=null)
{
string value = reqeust.Form[key];
pi.SetValue(model, HackType(value, pi.PropertyType), null);
}
}
return model;
}
todayclock 2011-09-08
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 dongxinxi 的回复:]
如果那些控件是嵌套的,名字会变掉的,可以考虑把Equlas改成EndsWith
不用遍历Form.AllKeys的,直接判断reqeust.Form[key],有值就SetValue
[/Quote]
这个我改了。现在基本可以用了。反之怎么做?昨天没想出来。
todayclock 2011-09-08
  • 打赏
  • 举报
回复
反过来加载值,还是一个一个写吧!
  • 打赏
  • 举报
回复
如果那些控件是嵌套的,名字会变掉的,可以考虑把Equlas改成EndsWith
不用遍历Form.AllKeys的,直接判断reqeust.Form[key],有值就SetValue
hwyqy 2011-09-07
  • 打赏
  • 举报
回复
纯粹帮顶了,太晚了,我不是夜猫
todayclock 2011-09-07
  • 打赏
  • 举报
回复
[Quote=引用 2 3 楼 kongwei521 的回复:]
[/Quote]
3楼那个我在试试!
蝶恋花雨 2011-09-07
  • 打赏
  • 举报
回复
http://www.cnblogs.com/henq/archive/2009/08/31/1557726.html
蝶恋花雨 2011-09-07
  • 打赏
  • 举报
回复
看下http://www.cnblogs.com/jacd/archive/2009/09/03/1559213.html这个
todayclock 2011-09-07
  • 打赏
  • 举报
回复
示例,有一定的局限性

<!--表单元素的name和实体属性相同 -->
<form id="form1" action="TestSubmitForm.aspx" method="post">
<input name="subFlag" name="subFlag" type="hidden" value="1" />
<input name="Title" type="text" /><br />
<input name="Content" type="text" /><br />
<input name="Author" type="text" /><br />
<input name="Button1" type="submit" value="button" />
</form>

cs文件

Article article = GetFormValue<Article>(Request);
可以获取到一个article对象

62,047

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧