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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T03:54:09+00:00 2026-05-21T03:54:09+00:00

I’ve read the links listed when I was creating this question. I think what

  • 0

I’ve read the links listed when I was creating this question. I think what I’m trying to do differs slightly. I am using ASP.NET MVC 2.

Apologies for the incoming wall of text. The most brief way I can think to ask this question is immediately below. After that I go into more of the why and how of the question.

[VERY QUICK POINT OF THE QUESTION]
How do I return a dictionary as a JSON result from an action and what jquery/javascript could be used in the onLoad event of the drop down lists to search that JSON for a named element and if it is found, return the corresponding value and pre-select that value in the drop down list?

[BACKGROUND]
I am working on creating a project for work where the user select an option from 2 initial drop downs. The first drop contains a list of pdf file names. The second drop downs contains a list of database queries associated with an alias name (they do not see sql…they see the name of the corresponding stored procedure or view). After the user has made these 2 selections the page reloads with many more drop downs below in 2 columns.

On the left column is a list of drop downs that all contain the same content: a list of the names of the text fields contained within the selected pdf (I am using CeTe software’s Dynamic PDF to handle pdf interaction). On the right column will be a list of drop downs containing the field names of the selected query. Both columns will have an equal number of drop downs; the total number will be the number of fields on the pdf.

[PROBLEM]
I can load the page fine and populate the drop downs with their respective data. The problem comes after the submit button is clicked and the data is posted. Since the drop downs are created dynamically, I’m using a naming convention to link which selections belong to the pdf fields and which belong to the db fields. On the action side, I iterate over the FormCollection and create a dictionary where the key is the name of the element posted and the value is the value of the element that was submitted. I only add entries to the dictionary for elements that have a non-null, non-empty value.

So far, I have all of that working. My issue, as you’ve probably guessed, is maintaining the selections when the page returns from the Action back to the view. I’ve got some really mutilated code that is close, but probably a world away from a good solution.

I will post the code I have that makes sense below. Another idea I have, that I’ve no clue how to go about doing is to put a function in the onLoad event of the drop down lists and goes to a json action and sends posts the name of the drop down calling the action/script (whatever this becomes). If the name submitted exists in the dictionary where I’m storing the previously submitted names & values of the drop down lists, the corresponding value that should be pre-selected is returned; otherwise nothing is returned and none of the drop down’s values are pre-selected.

I don’t know much about JSON, but if I could return the previously posted name/value dictionary as a JSON result to the page, I could use jquery to parse the json and handle the preselection where needed on the drop down.

See my current code below. It could be emulated by just creating stubs data for the SelectItemLists if you so cared. The current issue is that whatever the last name/value element is in the pre-selected dictionary is what is pre-selected for the rest of the list of drop downs.

Ex:

Selected values:

A                   FirstName
B                   LastName
C                   SSN
<no selection>      <no selection>
<no selection>      <no selection>
<no selection>      <no selection>
<no selection>      <no selection>

If the above is submitted, the result is the following:

A                   FirstName
B                   LastName
C                   SSN
C                   SSN
C                   SSN
C                   SSN
C                   SSN

Current code:

           <% using (Html.BeginForm())  {  %>                 

   <%  
       List<SelectListItem> mainPdfFieldNames = (List<SelectListItem>)ViewData["PdfFieldNames"];
       List<SelectListItem> mainDbFieldNames = (List<SelectListItem>)ViewData["DbFieldNames"];
       Dictionary<String, String> fieldValue = (Dictionary<string, string>)ViewData["selectedFieldValues"] ?? new Dictionary<string, string>();

   %>

   <% for (int i = 0; i < mainPdfFieldNames.Count; i++)
{

        String pdfPreselectValue = string.Empty; 
        String dbPreselectValue = string.Empty;
        fieldValue.TryGetValue("PdfFieldNames" + i.ToString(), out pdfPreselectValue);
        fieldValue.TryGetValue("DbFieldNames" + i.ToString(), out dbPreselectValue);
        IDictionary<string, object> pdfHtmlAttrib = new Dictionary<string, object>();
        IDictionary<string, object> dbHtmlAttrib = new Dictionary<string, object>();
        List<SelectListItem> pdfFieldNames = mainPdfFieldNames;
        List<SelectListItem> dbFieldNames = mainDbFieldNames;
%>
       <%=  Html.Encode(" PDF Selected value: " + pdfPreselectValue )  %>
       <%=  Html.Encode(" DB Selected value: " + dbPreselectValue) %>
<%        
        if (!String.IsNullOrEmpty(pdfPreselectValue))
        {
            pdfFieldNames.Find(p => p.Value.Equals(pdfPreselectValue)).Selected = true;
            pdfHtmlAttrib.Add("selected", null);
        }

        if (!String.IsNullOrEmpty(dbPreselectValue))
        {
            dbFieldNames.Find(p => p.Value.Equals(dbPreselectValue)).Selected = true;
            dbHtmlAttrib.Add("selected", null);
        }
          %>

       Select PDF Field: <%=Html.DropDownList("PdfFieldNames" + i.ToString(), pdfFieldNames, "Select PDF Field", pdfHtmlAttrib)%>
       &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
       Select Database Field: <%=Html.DropDownList("DbFieldNames" + i.ToString(), dbFieldNames, "Select Database Field", dbHtmlAttrib)%>
       <br />
   <%
      }%>

   <input type="submit" value="Submit" />

   <% } %>  

MAJOR EDIT 1 OMITTED TO MAKE ROOM FOR SOLUTION

I’ve decided to go a different route with this. I think this might be more simplified in the end. After the view posts to the action, I create a string variable from the Dictionary I was previously using. This string is put in ViewData and on the page is a hidden field. I have a javascript function that grabs this field and parses it. The nameValue field has the format of “fieldname,fieldvalue;fieldname,fieldvalue;fieldname,fieldvalue”.

I am posting some test code below. It’s actually a full html page that I’m using to develop this process bit by bit. My only remaining issue is triggering code to load after the page has been loaded. This code will select all drop downs on the page, iterate over them, and pass the name of the drop down to the function I’ve created that handles pre-selecting a value on the drop down based on it’s name and whether that name exists in the nameValue hidden field on the page.

  • 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-21T03:54:10+00:00Added an answer on May 21, 2026 at 3:54 am

    SOLVED ISSUE

    I expanded this sample html page to illustrate the issue better.

    A javascript error is being thrown somewhere, but I haven’t located it yet. The code still works and the correct drop downs are pre-selected based on the data in the postedFieldValues hidden field.

    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
      <head>
        <meta name="generator" content=
        "HTML Tidy for Windows (vers 14 February 2006), see www.w3.org">
        <title></title>
      </head>
      <body>
        <form>
          <input id="postedFieldValues" name="postedFieldValues" type="hidden"
          value=
          "PdfFieldNames0,f1-10;DbFieldNames0,LastName;PdfFieldNames1,f1-12;DbFieldNames1,LoanNumber;PdfFieldNames2,f1-15;DbFieldNames2,SSN;">
        </form>
        <p>
          Values stored in the postedFieldValues hidden field for this example
          are:<br>
          "PdfFieldNames0,f1-10;DbFieldNames0,LastName;PdfFieldNames1,f1-12;DbFieldNames1,LoanNumber;PdfFieldNames2,f1-15;DbFieldNames2,SSN;"
        </p><br>
        <script type="text/javascript" src=
        "http://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js">
    </script><script type="text/javascript">
                    if(typeof Jquery == 'undefined')
                                    document.write(unescape("%3Cscript src ='jquery-1.4.4.js'" + "type='text/javascript'%3E%3C/script%3E"));
    
    
                            function PreselectValue(name) {
    
                                    var values = $("[id=postedFieldValues]");
                                    var ddl = $("[id="+name+"]");
    
                                    //alert(values.val());
                                    //alert(ddl.val());
    
                                    if( values == null || values == '')
                                            return;   
    
                                    var pairs = values.val().split(";");
    
                                    for (var i = 0; i < pairs.length; i++) {
                                            //name is in index 0
                                            //value is in index 1
                                            var individualNameValuePair = pairs[i].split(",");
                                            if (name == individualNameValuePair[0]) {
                                                    ddl.val(individualNameValuePair[1]);
                                            }        
                                    }       
                            }
    
        </script>Select PDF Field: <select id="PdfFieldNames0" name=
        "PdfFieldNames0">
          <option value="">
            Select PDF Field
          </option>
          <option value="f1-1">
            f1-1
          </option>
          <option value="f1-10">
            f1-10
          </option>
          <option value="f1-11">
            f1-11
          </option>
          <option value="f1-12">
            f1-12
          </option>
          <option value="f1-13">
            f1-13
          </option>
          <option value="f1-14">
            f1-14
          </option>
          <option value="f1-15">
            f1-15
          </option>
          <option value="f1-16">
            f1-16
          </option>
        </select>           Select Database Field: <select id="DbFieldNames0" name=
        "DbFieldNames0">
          <option value="">
            Select Database Field
          </option>
          <option value="FirstName">
            FirstName
          </option>
          <option value="LastName">
            LastName
          </option>
          <option value="SSN">
            SSN
          </option>
          <option value="LoanNumber">
            LoanNumber
          </option>
        </select><br>
        Select PDF Field: <select id="PdfFieldNames1" name="PdfFieldNames1">
          <option value="">
            Select PDF Field
          </option>
          <option value="f1-1">
            f1-1
          </option>
          <option value="f1-10">
            f1-10
          </option>
          <option value="f1-11">
            f1-11
          </option>
          <option value="f1-12">
            f1-12
          </option>
          <option value="f1-13">
            f1-13
          </option>
          <option value="f1-14">
            f1-14
          </option>
          <option value="f1-15">
            f1-15
          </option>
          <option value="f1-16">
            f1-16
          </option>
        </select>           Select Database Field: <select id="DbFieldNames1" name=
        "DbFieldNames1">
          <option value="">
            Select Database Field
          </option>
          <option value="FirstName">
            FirstName
          </option>
          <option value="LastName">
            LastName
          </option>
          <option value="SSN">
            SSN
          </option>
          <option value="LoanNumber">
            LoanNumber
          </option>
        </select><br>
        Select PDF Field: <select id="PdfFieldNames2" name="PdfFieldNames2">
          <option value="">
            Select PDF Field
          </option>
          <option value="f1-1">
            f1-1
          </option>
          <option value="f1-10">
            f1-10
          </option>
          <option value="f1-11">
            f1-11
          </option>
          <option value="f1-12">
            f1-12
          </option>
          <option value="f1-13">
            f1-13
          </option>
          <option value="f1-14">
            f1-14
          </option>
          <option value="f1-15">
            f1-15
          </option>
          <option value="f1-16">
            f1-16
          </option>
        </select>           Select Database Field: <select id="DbFieldNames2" name=
        "DbFieldNames2">
          <option value="">
            Select Database Field
          </option>
          <option value="FirstName">
            FirstName
          </option>
          <option value="LastName">
            LastName
          </option>
          <option value="SSN">
            SSN
          </option>
          <option value="LoanNumber">
            LoanNumber
          </option>
        </select><br>
        Select PDF Field: <select id="PdfFieldNames3" name="PdfFieldNames3">
          <option value="">
            Select PDF Field
          </option>
          <option value="f1-1">
            f1-1
          </option>
          <option value="f1-10">
            f1-10
          </option>
          <option value="f1-11">
            f1-11
          </option>
          <option value="f1-12">
            f1-12
          </option>
          <option value="f1-13">
            f1-13
          </option>
          <option value="f1-14">
            f1-14
          </option>
          <option value="f1-15">
            f1-15
          </option>
          <option value="f1-16">
            f1-16
          </option>
        </select>           Select Database Field: <select id="DbFieldNames3" name=
        "DbFieldNames3">
          <option value="">
            Select Database Field
          </option>
          <option value="FirstName">
            FirstName
          </option>
          <option value="LastName">
            LastName
          </option>
          <option value="SSN">
            SSN
          </option>
          <option value="LoanNumber">
            LoanNumber
          </option>
        </select><br>
        Select PDF Field: <select id="PdfFieldNames4" name="PdfFieldNames4">
          <option value="">
            Select PDF Field
          </option>
          <option value="f1-1">
            f1-1
          </option>
          <option value="f1-10">
            f1-10
          </option>
          <option value="f1-11">
            f1-11
          </option>
          <option value="f1-12">
            f1-12
          </option>
          <option value="f1-13">
            f1-13
          </option>
          <option value="f1-14">
            f1-14
          </option>
          <option value="f1-15">
            f1-15
          </option>
          <option value="f1-16">
            f1-16
          </option>
        </select>           Select Database Field: <select id="DbFieldNames4" name=
        "DbFieldNames4">
          <option value="">
            Select Database Field
          </option>
          <option value="FirstName">
            FirstName
          </option>
          <option value="LastName">
            LastName
          </option>
          <option value="SSN">
            SSN
          </option>
          <option value="LoanNumber">
            LoanNumber
          </option>
        </select><br>
        Select PDF Field: <select id="PdfFieldNames5" name="PdfFieldNames5">
          <option value="">
            Select PDF Field
          </option>
          <option value="f1-1">
            f1-1
          </option>
          <option value="f1-10">
            f1-10
          </option>
          <option value="f1-11">
            f1-11
          </option>
          <option value="f1-12">
            f1-12
          </option>
          <option value="f1-13">
            f1-13
          </option>
          <option value="f1-14">
            f1-14
          </option>
          <option value="f1-15">
            f1-15
          </option>
          <option value="f1-16">
            f1-16
          </option>
        </select>           Select Database Field: <select id="DbFieldNames5" name=
        "DbFieldNames5">
          <option value="">
            Select Database Field
          </option>
          <option value="FirstName">
            FirstName
          </option>
          <option value="LastName">
            LastName
          </option>
          <option value="SSN">
            SSN
          </option>
          <option value="LoanNumber">
            LoanNumber
          </option>
        </select><br>
        Select PDF Field: <select id="PdfFieldNames6" name="PdfFieldNames6">
          <option value="">
            Select PDF Field
          </option>
          <option value="f1-1">
            f1-1
          </option>
          <option value="f1-10">
            f1-10
          </option>
          <option value="f1-11">
            f1-11
          </option>
          <option value="f1-12">
            f1-12
          </option>
          <option value="f1-13">
            f1-13
          </option>
          <option value="f1-14">
            f1-14
          </option>
          <option value="f1-15">
            f1-15
          </option>
          <option value="f1-16">
            f1-16
          </option>
        </select>           Select Database Field: <select id="DbFieldNames6" name=
        "DbFieldNames6">
          <option value="">
            Select Database Field
          </option>
          <option value="FirstName">
            FirstName
          </option>
          <option value="LastName">
            LastName
          </option>
          <option value="SSN">
            SSN
          </option>
          <option value="LoanNumber">
            LoanNumber
          </option>
        </select><br>
        Select PDF Field: <select id="PdfFieldNames7" name="PdfFieldNames7">
          <option value="">
            Select PDF Field
          </option>
          <option value="f1-1">
            f1-1
          </option>
          <option value="f1-10">
            f1-10
          </option>
          <option value="f1-11">
            f1-11
          </option>
          <option value="f1-12">
            f1-12
          </option>
          <option value="f1-13">
            f1-13
          </option>
          <option value="f1-14">
            f1-14
          </option>
          <option value="f1-15">
            f1-15
          </option>
          <option value="f1-16">
            f1-16
          </option>
        </select>           Select Database Field: <select id="DbFieldNames7" name=
        "DbFieldNames7">
          <option value="">
            Select Database Field
          </option>
          <option value="FirstName">
            FirstName
          </option>
          <option value="LastName">
            LastName
          </option>
          <option value="SSN">
            SSN
          </option>
          <option value="LoanNumber">
            LoanNumber
          </option>
        </select><br>
        <br>
        <form>
          PAGE HAS LOADED. <script type="text/javascript">
        $(document).ready(function () {
                            //alert('testing');
                            $('select').each(function(index) {
                                    var id = $(this).attr('id');;
                                    PreselectValue(id);
                            });
                            //alert('after iterate');
                    });
          </script>
        </form>
      </body>
    </html>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I am trying to loop through a bunch of documents I have to put
I'm making a simple page using Google Maps API 3. My first. One marker
I have some data like this: 1 2 3 4 5 9 2 6
I have a bunch of posts stored in text files formatted in yaml/textile (from

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.