Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6072045
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T10:06:59+00:00 2026-05-23T10:06:59+00:00

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

  • 0

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);
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T10:07:00+00:00Added an answer on May 23, 2026 at 10:07 am

    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);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to get an entity with its sub-properties. How can I do that
I can get a list of running threads from Process.GetCurrentProcess().Threads, but I need to
I know I can do this foreach (PropertyInfo property in myobject.GetType().GetProperties()) { if (property.DeclaringType.ToString()
I can get easily see what projects and dlls a single project references from
I can get both System.Net.Mail and System.Web.Mail to work with GMail, but I can't
Logging can get complicated, quickly. Considering that you have some code, how do you
I can get the executable location from the process, how do I get the
I can get the element like this $(#txtEmail) but I'm not sure how to
XAML lets me attach properties to types that are not derived from DependencyObject. For
I know this is crazy, but IE can drive one to do crazy things.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.