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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T07:33:58+00:00 2026-05-23T07:33:58+00:00

EditorFor() can take an object additionalViewData parameter which the typical method to populate is

  • 0

EditorFor() can take an object additionalViewData parameter which the typical method to populate is something like:

EditorFor(model => model.PropertyName, new { myKey = "myValue" })

How can I inspect the contents of additionalViewData, add or append to existing value for a key, etc in a custom HTML Helper?

I’ve tried these approaches:

  • convert to Dictionary<string, object>() and add/append values: doesn’t work as it looks like the implementation of EditorFor in MVC uses new RouteValueDictionary(additionalViewData) which embeds the dictionary within a dictionary
  • convert to RouteValueDictionary using new RouteValueDictionary(additionalViewData) but that has same (or very similar) issue as above

I’m also open to “you’re doing it wrong” — maybe I’m missing a simpler approach. Keep in mind what I’m trying to do is write an HTML helper that is reusable and adds some values to the additionalViewData to be used by custom views. Some of the values depend on metadata from the property so it is not quite so easy as just use a bunch of different templates.

Update with example of what I’m doing:

    public static MvcHtmlString myNullableBooleanFor<TModel, TValue>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TValue>> choice, string templateName, object additionalViewData)
    {            
        ModelMetadata metadata = ModelMetadata.FromLambdaExpression(choice, htmlHelper.ViewData);

        /*
    here need to add to additionalViewData some key values among them:
    { propertyName, metadata.PropertyName }

     */

        StringBuilder sb = new StringBuilder();
        sb.AppendLine(htmlHelper.EditorFor(choice, templateName, additionalViewData).ToString());
        MvcHtmlString validation = htmlHelper.ValidationMessageFor(choice);
        if (validation != null)
            sb.AppendLine(validation.ToString());
        return new MvcHtmlString(sb.ToString());
    }

Update with what happens when I convert the anonymous object to a Dictionary<string, object>() and pass that dictionary to EditorFor():

I put a break point in the Razor view and examined ViewData. It appears that the dictionary passed into EditorFor() is put inside another Dictionary<string, object>(). In the “Immediate Window”, ViewData looks like this:

ViewData.Keys
Count = 4
    [0]: "Comparer"
    [1]: "Count"
    [2]: "Keys"
    [3]: "Values"

See how the dictionary has the contents of a dictionary within it? Yes, the actual data is in that inner dictionary however unsurprisingly, this doesn’t work.

Added bounty.

  • 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-23T07:33:59+00:00Added an answer on May 23, 2026 at 7:33 am

    If I understand correctly, you are trying to iterate over properties of an anonymous type. If so: How do I iterate over the properties of an anonymous object in C#?

    [Edit]
    Ok, it’s more than that. This is really bothering me now because I love C# and it won’t let me do what Python does, which I also love. So here’s a solution, if you are using C# 4 but it’s messy and will work for a similar problem I have but maybe not exactly for you:

        // Assume you've created a class to hold commonly used additional view data
        // called MyAdditionalViewData. I would create an inheritance hierarchy to
        // contain situation-specific (area-specific, in my case) additionalViewData.
    class MyAdditionalViewData
    {
        public string Name { get; set; }
    
        public string Sound { get; set; }
    }     
    
    /* In your views */
    // Pass an instance of this class in the call to your HTML Helper
    EditorFor(model => model.PropertyName, 
        new { adv = new MyAdditionalViewData { Name = "Cow", Sound = "Moo" } }) ;               
    
        /* In your HTML helper */
        // Here's the C# 4 part that makes this work.
    dynamic bovine = new ExpandoObject();
    
    // Copy values
    var adv = x.adv;
    if (adv.Name != null) bovine.Name = adv.Name;
    if (adv.Sound != null) bovine.Sound = adv.Sound;
    
    // Additional stuff
    bovine.Food = "Grass";
    bovine.Contents = "Burgers";
    
        // When the MVC framework converts this to a route value dictionary
        // you'll get a proper object, not a dictionary within a dictionary
    var dict = new RouteValueDictionary(bovine);
    

    There has got to be a better way.

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

Sidebar

Related Questions

I have something for which I can't find the right syntax: /Views/Shared/EditorTemplates/Component.cshtml @model Website.Models.Component
Why can't I pass in html attributes to EditorFor() ? eg; <%= Html.EditorFor(model =>
How can we access the model's property name when using EditorFor() with a template
<%: Html.EditorFor(model => model.date)%> How can I make this code display an empty textbox
How can I get access to the HTML extensions ...For like LabelFor, EditorFor, ValidationMessageFor
I've just read about MVC3's new CompareAttribute that you can apply to a model's
@using (Html.BeginForm(Edit, MyController, FormMethod.Post, new { enctype=multipart/form-data})) { @Html.EditorFor(model => model.Name) <input type=file name=fileUpload
So far we can use Html.EditorFor() to dynamically render the appropriate template for a
What is ASP.Net MVC support for Editor.For(model) and where can I read more about
Anyone know how you can call a functional equivalent to Html.EditorFor() from the Controller?

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.