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

  • Home
  • SEARCH
  • 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 805945
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T00:10:00+00:00 2026-05-15T00:10:00+00:00

In my interface I have a list of text boxes, something like this :

  • 0

In my interface I have a list of text boxes, something like this :
http://screencast.com/t/YjIxNjUyNmU

The number of textboxes is unknown as each of them is associated with a Template.
In my page, the goal is to associate a number to some of those templates.

Here is a sample HTML code :

<%  // loop on the templates
    foreach(ITemplate template in templates)
    {
        // get the content from the input dictionary
        int val;
        content.TryGetValue(template.Id, out val);
        // convert it as a string
        string value = ((val > 0) ? val.ToString() : string.Empty);

        // compute the element name/id (for dictionary binding)
        string id = ??????????
        string name = ??????????????
%>
        <label for="<%= name %>"><%= template.Name %></label>
        <input type="text" id="<%= id %>" name="<%= name %>" value="<%= value %>" />
        <br />
<%  }
%>

What I expect, in my controller, is to get a IDictionary where the first int is the template ID , and the other is the count given by the user.

Here is what I want :

public ActionResult Save(int? id, Dictionary<int, int> countByTemplate)

I tried a lot of things but nothing worked. I tried to read the sources but it’s a maze, and I’m getting a headhache trying to get information about model binding.

Questions :

  • is there a good ressource on how the modelbinding works ?
    I’d like someting exhaustive, I’m tired of the 84093043 blogs that talk about a given specific example.
  • how can I build my HTML, using to get a IDictionary (or even a IDictionary in my controller’s action ?

Thanks a lot for your help

  • 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-15T00:10:01+00:00Added an answer on May 15, 2026 at 12:10 am

    Ok… Thanks to Levi I was able to get on a solution.
    Not the cleaner one, but it works.

    The HTML should be written this way:

    <%
    int counter = 0;
    // loop on the templates
    foreach(ITemplate template in templates)
    {
            // get the value as text
            int val;
            content.TryGetValue(template.Id, out val);
            var value = ((val > 0) ? val.ToString() : string.Empty);
    
            // compute the element name (for dictionary binding)
            string id = "cbts_{0}".FormatMe(template.Id);
            string dictKey = "cbts[{0}].Key".FormatMe(counter);
            string dictValue = "cbts[{0}].Value".FormatMe(counter++);
    %>
            <input type="hidden" name="<%= dictKey %>" value="<%= template.Id %>" />
            <input type="text" id="<%= id %>" name="<%= dictValue %>" value="<%= value %>" />
            <label for="<%= id %>"><%= template.Name %></label>
            <br />
    <%  }
    %>
    

    I had to add a hidden field to store the value.
    I introduced a ‘fake’ counter just to loop over the dictionary the way ASP.Net MVC wants it.
    As a result I got a dictionary filled with my values and ‘0’ when the textbox is empty.

    Another problem appeared: the ModelState was considered not valid because “a value is required”. I don’t want my values to be required, but looking at the modelbinder code, I did not found a way to tell the binder that a value is NOT required.

    So I tricked the ModelState in my controller, removing all error, like this:

    public ActionResult Save(int? id, Dictionary<int, int> cbts)
    {
        // clear all errors from the modelstate
        foreach(var value in this.ModelState.Values)
            value.Errors.Clear();
    

    Well… I effectively got a solution, but the HTML is kind of ugly now, and counterintuitive (using an index to loop over a non-indexed collection ??).
    And I need to trick each time I’ll use this kind of binding to have it all work properly.

    So I will now open a new post to make dictionary binder something better.
    Here it is: ASP.Net MVC 2 – better ModelBinding for Dictionary<int, int>

    EDIT – There is a cleaner solution, thanks to Pavel Chuchuva.

    In the controller code, use a nullable int as value for the dictionary.
    A bit more code to add, but much cleaner.

    public ActionResult Save(int? id, Dictionary<int, int?> cbts)
    {
        // this is our final dictionary<int, int>
        Dictionary<int, int> cbtsFinal = new Dictionary<int, int>();
        // loop on the dicitonary with nullable values
        foreach(var key in cbts.Keys)
        {
            // if we have a value
            if(cbts[key].HasValue)
                // then put it in the final dictionary
                cbtsFinal.Add(key, cbts[key].Value);
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a list of items with the an interface like this public interface
The (command line) interface I have in mind is like so: watching FILE+ do
I have a listview control on an .aspx page. Inside this list view i
This isn't a real fluent interface. I have an object which builds up a
I have ControlA which accepts an IInterfaceB which has a property of type List<unknownType>
If I have interface IFoo, and have several classes that implement it, what is
Suppose we have: interface Foo { bool Func(int x); } class Bar: Foo {
Lets say you have interface definition. That interface can be Operation . Then you
I'm currently writing a class that implements the SeekableIterator interface and have run into
I have an Interface called IStep that can do some computation (See Execution in

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.