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 7024897
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T23:54:10+00:00 2026-05-27T23:54:10+00:00

I have a following problem. I have a class in the entity framework model

  • 0

I have a following problem. I have a class in the entity framework model that has properties e.g.:

class Company{
    public string Name {get; set};
    public string Address {get; set};
    public string Email{get; set};
    public string WebSite {get; set};
}

I have configuration in database that defines whether or not some field should be shown or not e.g:

  • Name: show
  • Address: show
  • Email: hide
  • Website: hide

This is dynamic and all fields are referenced by name.

When I display object in view. It would be nice to convert somehow a single object to some dictionary where key will be property name and value will be property value so I can check for every field by name whether it should be shown or not (maybe in some for-each loop) e.g.:

CompanyDetails.cshtml

<h2>Company Details</h2>
@foreach(var property in modelDictionary.Keys){

    @if(IsVisible(property))
        @Html.Raw( modelDictionary[property] )
}

What is the best way to convert a single object from entity framework model to dictionary of properties? Should I convert it from object to dictionary in the controller action or use model metadata somehow in the view?

I can use reflection on the Company class and find all properties in the class so I can populate dictionary, but this looks like too old-school solution, so I wander is there any better way to do this?

Thanks

  • 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-27T23:54:11+00:00Added an answer on May 27, 2026 at 11:54 pm

    You could use a RouteValueDictionary which allows you to convert an object into a dictionary:

    public class Company
    {
        public string Name { get; set; }
        public string Address { get; set; }
        public string Email { get; set; }
        public string WebSite { get; set; }
    }
    
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            var model = new Company
            {
                Address = "some address",
                Email = "some email",
                Name = "some name",
                WebSite = "some website"
            };
            return View(new RouteValueDictionary(model));
        }
    }
    

    and in the view:

    @model RouteValueDictionary
    
    <h2>Company Details</h2>
    @foreach (var item in Model)
    {
        if (IsVisible(item.Key))
        {
            <div>@item.Value</div>
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following model entity: public class ScheduledTask { public virtual int ScheduledTaskId
If i have the following entity: public class PocoWithDates { public string PocoName {
I have the following Entity Framework POCO classes: public class Customer { public int
Today I ran into the following problem with NUnit. I have a class, that
I have a problem with the following Linq query using Entity Framework: from o
Here is my problem in C# : I have the following classes: public class
I'm using MySQL and have the following entity: class MyEntity { @Id @GeneratedValue(strategy=IDENTITY) @Column(name=id)
I currently have an Entity Framework model that collects data from a legacy database
I have defined some models like this (Entity Framework Code-First): public class A {
I have the following two entities: @Entity public class SupermarketChain { @Id @GeneratedValue(strategy =

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.