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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T15:40:16+00:00 2026-06-03T15:40:16+00:00

I have a model that contains a dictionary property. (this has been distilled from

  • 0

I have a model that contains a dictionary property. (this has been distilled from a larger project into this example, which I have confirmed still has the same issue)

public class TestModel
{
    public IDictionary<string, string> Values { get; set; }

    public TestModel()
    {
        Values = new Dictionary<string, string>();
    }
}

a controller

public class TestController : Controller
{
    public ActionResult Index()
    {
        TestModel model = new TestModel();
        model.Values.Add("foo", "bar");
        model.Values.Add("fizz", "buzz");
        model.Values.Add("hello", "world");

        return View(model);
    }

    [HttpPost]
    public ActionResult Index(TestModel model)
    {
        // model.Values is null after post back here.
        return null; // I set a break point here to inspect 'model'
    }
}

and a view

@using TestMVC.Models
@model TestModel
@using (Html.BeginForm())
{
    @Html.EditorFor(m => m.Values["foo"]);
    <br />
    @Html.EditorFor(m => m.Values["fizz"]);
    <br />
    @Html.EditorFor(m => m.Values["hello"]);
    <br />
    <input type="submit" value="submit" />
}

This renders to the browser like this:

<input class="text-box single-line" id="Values_foo_" name="Values[foo]" type="text" value="bar" />

The problem I’m having is that the dictionary is null on the model after postback.

  • Am I doing this right, or is there a better way?

I need to have some kind of key-value storage, as the fields on my form are variable, so I can’t use a POCO model.

  • 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-06-03T15:40:17+00:00Added an answer on June 3, 2026 at 3:40 pm

    As @Blast_Dan and @gprasant mentioned, the model binder is expecting the name attribute of the input element to be in the format Property[index].Value, where index is an int and Value is one of the properties on the KeyValuePair class.

    Unfortunately, @Html.EditorFor generates this value in the wrong format. I wrote an HtmlHelper extension to transform the name attribute to the correct format:

    public static IHtmlString DictionaryEditorFor<TModel, TProperty, TKey, TValue>(this HtmlHelper<TModel> Html, Expression<Func<TModel, TProperty>> expression, IDictionary<TKey, TValue> dictionary, DictionaryIndexRetrievalCounter<TKey, TValue> counter, string templateName, object additionalViewData)
    {
        string hiddenKey = Html.HiddenFor(expression).ToHtmlString();
        string editorValue = Html.EditorFor(expression, templateName, additionalViewData).ToHtmlString();
        string expText = ExpressionHelper.GetExpressionText(expression);
        string indexText = expText.Substring(expText.IndexOf('[')).Replace("[", string.Empty).Replace("]", string.Empty);
    
        KeyValuePair<TKey, TValue> item = dictionary.SingleOrDefault(p => p.Key.ToString() == indexText);
        int index = counter.GetIndex(item.Key);
    
        string key = hiddenKey.Replace("[" + indexText + "]", "[" + index + "].Key").Replace("value=\"" + item.Value + "\"", "value=\"" + item.Key + "\"");
    
        string value = editorValue.Replace("[" + indexText + "]", "[" + index + "].Value");
    
        return new HtmlString(key + value);
    }
    

    Because the integer index must follow these rules:

    1. Must start with 0

    2. Must be unbroken (you can’t skip from 3 to 5, for example)

    I wrote a counter class to handle getting the integer index for me:

    public class DictionaryIndexRetrievalCounter<TKey, TValue>
    {
        private IDictionary<TKey, TValue> _dictionary;
        private IList<TKey> _retrievedKeys;
    
        public DictionaryIndexRetrievalCounter(IDictionary<TKey, TValue> dictionary)
        {
            this._dictionary = dictionary;
            this._retrievedKeys = new List<TKey>();
        }
    
        public int GetIndex(TKey key)
        {
            if (!_retrievedKeys.Contains(key))
            {
                _retrievedKeys.Add(key);
            }
    
            return _retrievedKeys.IndexOf(key);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a class that contains data from some model. This class has metadata
I have a model window that contains expander control. This expander control when expanded
I have a model that contains a List<PhoneNumber> property. I use TryUpdateModel in my
I have a model that contains a collection, such as this: class MyModel {
I have a class that contains a property-value (property bag) dictionary beside normal properties.
I have a model that contains a foreign key value, then in the form
I have a model that contains one-to-one fields to other models. I over rid
I have a Model that contains a List of a custom type. I want
I have a model form that contains a DecimalField() with max_digits set to 5.
I have a view model that contains a Product class type and an IEnumerable<

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.