如何使用foreach将我的属性从模型中导入到我的视图中?

时间:2011-06-27 13:10:12

标签: c# asp.net-mvc razor ienumerable

如何使用foreach将我的属性从模型中导入到我的视图中?

我知道我可以使用@Html.EditorFor(model => model.ID)但在我的情况下这是不可能的,因为我对不同的模型使用一个View(从BaseModel继承)。

型号:

public class MyModel :  IEnumerable
{
    private PropertyInfo[] propertys 
    { 
        get
        {
            if (propertys != null) return propertys;

            string projectName = System.Reflection.Assembly.GetExecutingAssembly().GetName().Name;
            Type classtype = Type.GetType(string.Format("{0}.Models.{1}", projectName, FQModelname));
            PropertyInfo[] properties = classtype.GetProperties();

            return properties;
        }
    }

    public int ID { get; set; }

    public string Name { get; set; }

    //...

    public IEnumerator GetEnumerator()
    {
        return propertys.GetEnumerator();
    }
}

RazorView:

@foreach (var property in Model)
{
    // [Error] need Typeargument...?
    @Html.EditorFor(property);
}

2 个答案:

答案 0 :(得分:1)

您是否尝试过@Html.EditorForModel()而不是@Html.EditorFor() ??

答案 1 :(得分:0)

这可以做得更强,但至少这是一个快速实现的想法,你需要改进一些概念并为你的特定项目提供一些有用的东西。

void Main()
{
    BaseModel baseModelTest = new Concrete() { Test = "test property" };

    foreach ( var property in baseModelTest.EnumerateProperties())
    {
        var value = baseModelTest.GetPropertyValue(property.Name);
        value.Dump();
    }


}

public class EnumeratedProperty
{
    public string Name { get; private set; }
    public Type Type { get; private set; }
    public EnumeratedProperty(string PropertyName, Type PropertyType)
    {
        this.Name = PropertyName;
        this.Type = PropertyType;
    }
}

public abstract class BaseModel
{
    protected IEnumerable<PropertyInfo> PropertyInfoCache { get; set; }
    protected IEnumerable<EnumeratedProperty> EnumeratedPropertyCache { get; set; }
    protected BaseModel()
    {
        PropertyInfoCache = this.GetType().GetProperties();
        EnumeratedPropertyCache = PropertyInfoCache.Select(p=> new EnumeratedProperty(p.Name,p.GetType()));
    }
    public IEnumerable<EnumeratedProperty> EnumerateProperties()
    {
        return EnumeratedPropertyCache;
    }
    public object GetPropertyValue(string PropertyName)
    {
        var property = PropertyInfoCache.SingleOrDefault(i=>i.Name==PropertyName);
        if(property!=null)
            return property.GetValue(this,null);
        return null;
    }
}

public class Concrete : BaseModel
{
    public string Test { get; set; }
}

...

public static class ExtensionMethods
{
    public static MvcHtmlString EditorForProperty(this HtmlHelper html, BaseModel Model, EnumeratedProperty property)
    {
        // invoke the appropriate Html.EditorFor(...) method at runtime
        // using the type info availible in property.Type
        return ...
    }
}

...

@foreach (var property in Model.EnumerateProperties())
{
    // call the new extention method, pass the EnumeratedProperty type
    // and the model reference
    @Html.EditorForProperty(Model,property);
}