Click here to Skip to main content
13,001,514 members (59,971 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


2 bookmarked
Posted 29 Sep 2011

ASP.NET MVC – Binding to Dictionary

, 29 Sep 2011
Rate this:
Please Sign up or sign in to vote.
ASP.NET MVC - Binding to dictionary

Today, I came across a situation where I had to display a list of combo boxes inside a part of a complicated tab based form. The model of the view was a different object than this particular section I was trying to render. I had a different controller action to do the updates.

After Googling, I came across this article from Scott - ASP.NET Wire Format for Model Binding to Arrays, Lists, Collections, Dictionaries which turned out to be very helpful.

While implementing this all I need the binder to return something like a dictionary of int key with int value.

public ActionResult EditProjectAccess(int? id, Dictionary<int, int> ProjectAccess)

So in my View, I used the following snippet:

Var counter = 0;
foreach (var item in ProductGroups)
<legend>@item.GroupName</legend>@foreach (var pj in item.Products)

    <dd class="text"> @if (Model.ResourceId



			Manager</strong></text>    }
    else    {
        @Html.Hidden("ProjectAccess[" + counter + "].key", pj.ProductId)
        @(Html.DropDownList("ProjectAccess[" + counter + "].value",
          new SelectList(new List<Object>{
          new { value = "0″ , text = "No Access"  },
          new { value = "1″ , text = "Participant" },
          }, "value", "text", pj.HasAccess(Model.ResourceId)?0:1))) counter++;
    </dd>    </dl>    }

The key here was using the Hidden field for storing the key name, and the combo box for the value field. One thing though the counter values should be incremental, i.e., no gaps in sequence. Else on the model you will only get up to the sequenced item.

So now, I can easily get access to my dictionary list and figure what each of those combo values was selected to. The key can be string as well and the value can be complex object. Just follow Scott’s blog article.


This article, along with any associated source code and files, is licensed under The Creative Commons Attribution-ShareAlike 2.5 License


About the Author

Sazzad Hossain
Binary Quest
Australia Australia
Sazzad is working as a developer since 2001 in the Realestate Market in Melbourne. Likes to work in C# and .Net platform. Started an ISV Business in 2008 called Binary Quest.

Follow me on Linked In

You may also be interested in...

Comments and Discussions

GeneralMy vote of 5 Pin
Md. Rashim uddin29-Sep-11 20:49
memberMd. Rashim uddin29-Sep-11 20:49 
GeneralRe: My vote of 5 Pin
Sazzad Hossain30-Sep-11 1:01
memberSazzad Hossain30-Sep-11 1:01 
GeneralMy vote of 5 Pin
bindum3129-Sep-11 20:19
memberbindum3129-Sep-11 20:19 
GeneralRe: My vote of 5 Pin
Sazzad Hossain30-Sep-11 1:02
memberSazzad Hossain30-Sep-11 1:02 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170624.1 | Last Updated 29 Sep 2011
Article Copyright 2011 by Sazzad Hossain
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid