我怎样才能从模型我的属性到我查看在foreach? [英] How can I get my properties from a Model into my View with a foreach?

查看:83
本文介绍了我怎样才能从模型我的属性到我查看在foreach?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我怎样才能得到一个模型的属性我到我的查看与一个foreach?

我知道,我可以使用 @ Html.EditorFor(型号=> model.ID)但对我来说,这是不可能的,因为我用不同的一个视图模型(从BaseModel继承)。

型号:

 公共类为MyModel:IEnumerable的
{
    私人的PropertyInfo [] propertys
    {
        得到
        {
            如果(propertys!= NULL)回报propertys;            字符串PROJECTNAME = System.Reflection.Assembly.GetExecutingAssembly()的GetName()名称。
            类型CLASSTYPE = Type.GetType(的String.Format({0} .Models {1},项目名称,FQModelname));
            的PropertyInfo []属性= classtype.GetProperties();            返回性能;
        }
    }    公众诠释ID {搞定;组; }    公共字符串名称{;组; }    // ...    公众的IEnumerator的GetEnumerator()
    {
        返回propertys.GetEnumerator();
    }
}

RazorView:

  @foreach(以型号VAR属性)
{
    // [错误]需要Typeargument ...?
    @ Html.EditorFor(财产);
}


解决方案

这可以做到强类型化的,但是这是一个快速实现的想法,至少,你要细化一些概念,并得到的东西的工作您的具体项目。

 无效的主要()
{
    BaseModel baseModelTest =新混凝土(){测试=测试属性};    的foreach(在baseModelTest.EnumerateProperties)VAR财产()
    {
        VAR值= baseModelTest.GetPropertyValue(property.Name);
        value.Dump();
    }
}公共类EnumeratedProperty
{
    公共字符串名称{;私人集; }
    公共类型类型{搞定;私人集; }
    公共EnumeratedProperty(字符串属性名,类型属性类型)
    {
        this.Name =属性名;
        this.Type =属性类型;
    }
}公共抽象类BaseModel
{
    保护的IEnumerable<&的PropertyInfo GT; PropertyInfoCache {搞定;组; }
    保护的IEnumerable< EnumeratedProperty> EnumeratedPropertyCache {搞定;组; }
    保护BaseModel()
    {
        PropertyInfoCache = this.GetType()的GetProperties()。
        EnumeratedPropertyCache = PropertyInfoCache.Select(P =>新建EnumeratedProperty(p.Name,p.GetType()));
    }
    公共IEnumerable的< EnumeratedProperty> EnumerateProperties()
    {
        返回EnumeratedPropertyCache;
    }
    公共对象GetPropertyValue(字符串属性名)
    {
        VAR财产= PropertyInfoCache.SingleOrDefault(I => i.Name ==属性名);
        如果(财产!= NULL)
            返回property.GetValue(本,NULL);
        返回null;
    }
}公共类混凝土:BaseModel
{
    公共字符串测试{搞定;组; }
}

...

 公共静态类ExtensionMethods
{
    公共静态MvcHtmlString EditorForProperty(这个HTML的HtmlHelper,BaseModel型号,EnumeratedProperty属性)
    {
        //调用运行时适当Html.EditorFor(...)方法
        使用类型信息在property.Type菱//
        返回...
    }
}

...

  @foreach(在Model.EnumerateProperties VAR财产())
{
    //调用新的延伸方法,通过EnumeratedProperty类型
    //和模型参考
    @ Html.EditorForProperty(型号,特性);
}

How can I get my properties from a Model into my View with a foreach?

I know that I could use @Html.EditorFor(model => model.ID) but in my case this is not possible because I use one View for different Models (inherit from a BaseModel).

Model:

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);
}

解决方案

This could be done stronger typed but this is a quick implementation of the idea at least, you'll want to refine some of the concepts and get something working for your specific project.

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);
}

这篇关于我怎样才能从模型我的属性到我查看在foreach?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆