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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T01:25:11+00:00 2026-05-20T01:25:11+00:00

The idea (simplified) is to have user definable properties in ViewModel contained in a

  • 0

The idea (simplified) is to have user definable properties in ViewModel contained in a dictionary. Something along these lines:

public class MyViewModel
{
    [Required]
    public string Name { get; set; }

    [DisplayName("User address")]
    public string Address { get; set; }

    // ...

    public IDictionary<string, string> MetaData { get; set; }
}

Let’s say that MetaData contains several additional properties: PhoneNumber, Email, etc. that you can access with myViewModel.MetaData["PhoneNumber"].

What I would like to do is to be able to use those additional MetaData properties in Html helpers on View side, just like I would use normal properties.

So, in addition to using standard properties as:

Html.TextBox("Name")

I would also like to use those additional properties:

Html.TextBox("PhoneNumber")

My research lead me to inheriting from DataAnnotationsModelMetadataProvider (since it’s necessary to also support standard DataAnnotations attributes for standard properties) and trying to figure out what exactly to override there in order to inject additional properties as additional ModelMetadata elements, but I’m kind of stuck.

Am I on the right path? Any additional pointer that could help me here?

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-20T01:25:12+00:00Added an answer on May 20, 2026 at 1:25 am

    An alternative option might be to construct a dynamic object similar to ViewBag/ViewData in MVC 3. You would have an object which you could access via Model.MetaData.Foo and Foo would actually map to a key in your dictionary.

    The type which backs the ViewBag object is System.Web.Mvc.DynamicViewDataDictionary; this class is internal and sealed so you would have to make a custom implementation of it (unless there’s a better option I’m unaware of). A quick glance at the MVC 3 sources furnished this:

    internal sealed class DynamicViewDataDictionary : DynamicObject {
        private readonly Func<ViewDataDictionary> _viewDataThunk;
    
        public DynamicViewDataDictionary(Func<ViewDataDictionary> viewDataThunk) {
            _viewDataThunk = viewDataThunk;
        }
    
        private ViewDataDictionary ViewData {
            get {
                ViewDataDictionary viewData = _viewDataThunk();
                Debug.Assert(viewData != null);
                return viewData;
            }
        }
    
        // Implementing this function improves the debugging experience as it provides the debugger with the list of all
        // the properties currently defined on the object
        public override IEnumerable<string> GetDynamicMemberNames() {
            return ViewData.Keys;
        }
    
        public override bool TryGetMember(GetMemberBinder binder, out object result) {
            result = ViewData[binder.Name];
            // since ViewDataDictionary always returns a result even if the key does not exist, always return true
            return true;
        }
    
        public override bool TrySetMember(SetMemberBinder binder, object value) {
            ViewData[binder.Name] = value;
            // you can always set a key in the dictionary so return true
            return true;
        }
    }
    

    One possible advantage to this solution over modifying the ModelMetadataProvider is that you wouldn’t have to spend time building all the custom components for your scenario- the default providers should be sufficient.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a problem creating authentication part for my application. Below is the simplified
This is the simplified version of the problem: We have a table on an
The basic idea is very easy. Simplified you could say... a snake like line
I have a following HTML/CSS (simplified): <body> <style> body { margin: 0px; padding: 0px;
I have simplified my page and here what I have: <html> <head> <meta http-equiv=content-type
I am trying to retrieve the AST from scala souce file. I have simplified
im trying to solve this issue. I have a web site with this simplified
I have simplified my function to the following: create function [dbo].[UserSuperTeams](@ProjectId int) returns table
Using CouchDB , I currently have a document which represents an idea, you can
I'm trying to write an application using QNetworkManager. I have simplified the code down

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.