0

I am using Asp.Net MVC 4 C#

I want to use dictionary type so, on first page load in controller I tried this:

Dictionary<int, string> dictionary = new Dictionary<int, string>();
for (int i = 1; i < 51; i++)
{
    dictionary.Add(i, "blue");
} 

ViewBag.Colour = dictionary;

And in View I am passing it back through Hidden field

@Html.Hidden("Colour", (Dictionary<int,string>)@ViewBag.Colour)

Now on [HttpPost] I want to access all the values (Key,Value) from collection["Colour"] and may be update the Value part on some condition.

ekad
  • 14,436
  • 26
  • 44
  • 46
  • Here's a possible duplicate http://stackoverflow.com/questions/9385286/html-hiddenfor-does-not-work-on-lists-in-asp-net-mvc Basically, Hidden field can only hold a string. You can either serialize your dictionary (and deserialize when it comes back), or put it into ViewBag, or cache on the server. – yu_sha Apr 15 '15 at 08:52
  • Please, show the code of your get and post Actions from your controller. – zed Apr 16 '15 at 19:59
  • I have put the entire dictionary in a session and managed to retrieve it on postback... thank you. – Prutha Kalangutkar Apr 17 '15 at 06:11

0 Answers0