C# ASP.NET MVC 3动态表单生成
我试图在运行时生成一个表单。我最终采用了这种方法C# ASP.NET MVC 3动态表单生成,c#,asp.net,asp.net-mvc-3,reflection,razor,C#,Asp.net,Asp.net Mvc 3,Reflection,Razor,我试图在运行时生成一个表单。我最终采用了这种方法 @using (Html.BeginForm()) { @foreach (var propertyInfo in typeof(MvcApplication1.Models.LogOnModel).GetProperties()) { if (propertyInfo.PropertyType == typeof(Boolean)) { Html.CheckBoxFor(m => new P
@using (Html.BeginForm()) {
@foreach (var propertyInfo in typeof(MvcApplication1.Models.LogOnModel).GetProperties()) {
if (propertyInfo.PropertyType == typeof(Boolean)) {
Html.CheckBoxFor(m => new PropertyWrapper<Boolean>(propertyInfo, Model).Property);
}
else if (propertyInfo.PropertyType == typeof(String)) {
Html.TextBoxFor(m => new PropertyWrapper<String>(propertyInfo, Model).Property);
}
...
}
}
我得到以下错误
System.Reflection.TargetException: Non-static method requires a target.
因为PropertyRapper构造函数的实例参数为null。我错过什么了吗?这是可能的吗?忘记强类型、xxxforhelpers和lambda表达式。一旦你开始反思游戏,你必须一直玩到最后 xxxforhelpers使用非常简单的表达式,例如属性访问
m=>new PropertyRapperPropertyInfo,Model.Property远远超出了这些助手的能力。忘记强类型、xxxforhelpers和lambda表达式。一旦你开始反思游戏,你必须一直玩到最后 xxxforhelpers使用非常简单的表达式,例如属性访问
m=>new PropertyRapperPropertyInfo,Model.Property远远超出了这些助手的能力。为什么不使用非for方法@例如Html.CheckBox。只需将属性的名称传递给方法,模型绑定在回发后仍能工作为什么不使用非for方法@例如Html.CheckBox。只需将属性的名称传递给该方法,模型绑定在回发后仍然有效我没有意识到它们会自动绑定!如果你把你的评论作为一个答案,我会接受。我把它移到了一个答案。我没意识到它们会自动绑定!如果你把你的评论作为答案,我会接受的。我把它改成了答案。
System.Reflection.TargetException: Non-static method requires a target.