我已经完成了下一个解决方案:“ReflectionHelper”类反映模型属性,并且在“属性”变量中是模型字段 {{'Id',1},{'Name','Myname'} 等}:
public static class ReflectionHelper
{
public static void IterateProps(Object obj, string baseProperty, ref Dictionary<string,object> properties )
{
if (obj != null)
{
var baseType = obj.GetType();
var props = baseType.GetProperties();
foreach (var property in props)
{
var name = property.Name;
var propType = property.PropertyType;
if (propType.IsClass && propType.Name != "String")
{
IterateProps(property.GetValue(obj, null), baseProperty + "." + property.Name, ref properties);
}
else
{
properties.Add(baseProperty + "." + name, property.GetValue(obj, null));
}
}
}
}
}
鉴于我做了下一个:
@using (Html.BeginForm("MyStep1", "Wizard"))
{
var properties = new Dictionary<string, object>();
ReflectionHelper.IterateProps(Model, Model.GetType().Name,ref properties);
foreach (var property in properties)
{
<input type="hidden" name="@property.Key" value="@property.Value"/>
}
}
它是向导解决方案的一部分,稍后我将在 MVC 上写有关向导的文章,也许有人会发现有用。