簡體   English   中英

如何使用foreach將模型中的屬性輸入到視圖中?

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

如何使用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);
}

您是否嘗試過@Html.EditorForModel()而不是@Html.EditorFor()

可以用更強的類型來完成此操作,但這至少是該想法的快速實現,您將需要完善一些概念並為您的特定項目工作。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM